Issue with mailing list

Post here is you are having problems with any of the tutorials.
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Issue with mailing list

Post by pasqo83 »

Hi,

I usually go on yourtupe to learn about php, even if I am not coding but just to view it to get a better understanding of as I am currently trying to learn this.

I am now attempting to do a mailing list, the subscribe and unsubscribe.

I have created the core folder with init.inc.php and mail.inc.php inside it, and then the signup.php outside of the core folder (haven't got far as the send.mail and unsubscribe.php.

I have got as far as video 3 of your mailing list (http://www.youtube.com/watch?v=tTdcBnzuJhg)

I keep seeing this error

" Fatal error: Cannot redeclare add_user() (previously declared in /home/chatabl1/public_html/sj/core/mail.inc.php:15) in /home/chatabl1/public_html/sj/core/mail.inc.php on line 23 "

line 15 on mail.inc.php is
function add_user($firstname, $lastname, $email){
line 23 of mail.inc.php is
}
I keep checking the first 2 videos and re-following it but there doesnt seem to be an issue.

I am going to attempt to re-start it from scratch again and test stage by stage but can someone please shed some light on this?

p.s I am learning a great deal from yourself as well so many thanks for the videos, it really is amazing. :D

Many thanks
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Issue with mailing list

Post by ta2shop »

posting your sorce code could help people determin the problem better.
Image
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Issue with mailing list

Post by GenSwat »

I have had this happen before when there was a require that was supposed to be a require_once

and this error means that a function is being declared more than once per script execution.I am not sure why, and not having any source script its hard to say.often it is a file included more than once, or making another function in an application whos name already exists as a function.

make sure you don't have duplicated function name in function.php if it exists , in mail.inc.php, or you created a function and left it on the page.
One of my Favorites
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

pasqo83 wrote:p.s I am learning a great deal from yourself as well so many thanks for the videos, it really is amazing. :D
no problem :D

As for the the problem, the only thing I can think of that has not already been said is that your host has defined a custom function with this name already and you might just have to use a different function name. more likely though is that you are including the file twice.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Issue with mailing list

Post by nyo »

One more thing that I want to mention is that a problem is not always necessarily on the line that the error/warning/notice mentions. It could be above that line as well. That's why posting the whole code is better.
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

Hi All,

Thank you for your messages, I am just redoing it and testing it stage by stage to see if it is my coding. Either I need to go specsavers or I have put this in correctly. Here are the 2 files I have created, the first is mail.inc.php


MAIL.INC.PHP
<?php 

include ('core/init.inc.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php

// adds the given inforamtion to the database
function add_user($firstname, $lastname, $email){
	$firstname = mysql_real_escape_string($firstname);
	$lastname = mysql_real_escape_string($lastname);
	$email 	= mysql_real_escape_string($email);

	$result = mysql_query("INSERT INTO 'users' ('firstname', 'lastname', 'email') VALUES ('{$firstname}', '{$lastname}', '{$email}')");

	return ($result !== false) ? true : false;
}



// removes the email address from the table
function remove_user($email){
	$email 	= mysql_real_escape_string($email);

	mysql_query("DELETE FROM 'users' WHERE 'email' = '{$email}'");
}


// sends the message to all users
function mail_all ($subject, $meessage, $headers){
	
	$users = mysql_query("SELECT 'firstname', 'email' FROM 'users'");
	while (($user = mysql_fetch_asoc($users)) !== false){
		$body ="Hi, {$user['firstname']}\n\n{$message}\n\unnsubscribe:";
	
	
	mail ($user['email'], $subject, $body, $headers);


		
	}
	
}

?>

<body>
</body>
</html>


init.inc.php (for obvious reasons, i have taken out the username and password out of the php coding.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

mysql_connect ('localhost', '*******', '*********');
mysql_select_db ('chatabl1_spaghetti');


$path = dirname (__FILE__);

include("{$path}/core/mail.inc.php");

?>


</body>
</html>
Any help will be much appreciated. Again thank you in advanced :-)
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

You should not be including the init.inc.php file in the mail.inc.php file, that will be the cause of the problem.

Also both of these files should not have html in them :?
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

jacek wrote:You should not be including the init.inc.php file in the mail.inc.php file, that will be the cause of the problem.

Also both of these files should not have html in them :?

Thanks Jacek, I've deleted all the files and started all over again and following it step by step and its working out alot better, there were parts I obviously missed out. Im just about to go on video 3, almost working :D

I have't completed the tutorial yet but wanted to ask, can I style the html that I send to the emails within your coding?

Thanks :-)
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

pasqo83 wrote:can I style the html that I send to the emails within your coding?
you can if you add the Content-Type: text/html header. I made a basics video on it last week.
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

jacek wrote:
pasqo83 wrote:can I style the html that I send to the emails within your coding?
you can if you add the Content-Type: text/html header. I made a basics video on it last week.
Thats brilliant Jacek, many thanks :)

The coding is almost done, just having difficulty inserting into the database, it keeps coming up with the error message

" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' ('firstname', 'lastname', 'email') VALUES ('test', 'testing', 'test@test' at line 1 "

here is my mail.inc.php

<?php

// ADDS DETAILS INTO THE DB
function add_user($firstname, $lastname, $email){
	$firstname = mysql_real_escape_string($firstname);
	$lastname = mysql_real_escape_string($lastname);
	$email = mysql_real_escape_string($email);
	
	
	$result = mysql_query ("INSERT INTO 'users' ('firstname', 'lastname', 'email') VALUES ('{$firstname}', '{$lastname}', '{$email}')"); 
	
	echo mysql_error();
	
	return ($result !== false) ? true : false;	
}

// REMOVES THE EMAIL ADDRESS FROM THE TABLE
function remove_user($email){
	$email = mysql_real_escape_string($email);
	
	mysql_query("DELETE FROM 'users' WHERE 'email' = '{$email}'");
		
}


// SENDS MESSAGE TO ALL USRES
function mail_all($subject, $message, $headers){
	$users = mysql_query("SELECT 'firstname', 'email' FROM 'users'");
	
	while(($user = mysql_fetch_asoc($users))!== false){
	$body = "Hi, {$user['firstname']}\/\/{$message}\n\nUnsubscribe:";
	
	mail($user['email'], $subject, $body, $headers);
	
	}
	
}

?>

Really sorry to be a pain :-( im excited and annoyed at the same time.
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

if it helps, my SQL version is 5.0.91

p.s how long did it take you to learn php coding so well? :-)
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

pasqo83 wrote:" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' ('firstname', 'lastname', 'email') VALUES ('test', 'testing', 'test@test' at line 1 "
Table and column names need to be quoted with backticks ` not quotes ' ;) Looks good other than that :D I will start pointing that out in a few videos it seems to be a common problem.
pasqo83 wrote:p.s how long did it take you to learn php coding so well? :-)
3 years I think. I really wish I could remember :lol: It will be close to 3 years.
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

jacek wrote: Table and column names need to be quoted with backticks ` not quotes ' ;) Looks good other than that :D I will start pointing that out in a few videos it seems to be a common problem.
Didn't even realised the difference, yeah good idea to let us users know that (well at least any newbie viewing this, even if it is inside the text part of the video.


jacek wrote:3 years I think. I really wish I could remember :lol: It will be close to 3 years.
hahaha, and you can explain it with no issues, Im still trying to get my head over strings, arrays, if, else and mysql_connect, query etc... soo much to learn..... well it can save me money in the long run, plus it is soo exciting to learn :-)

One thing I meant to ask. What is the security like in php? Sorry sounds quite like a newbie question but I have never understood, is it easy to bypass php or is it quite secure? :D
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

pasqo83 wrote:One thing I meant to ask. What is the security like in php? Sorry sounds quite like a newbie question but I have never understood, is it easy to bypass php or is it quite secure? :D
There have been bugs in the past that cause security problem in php. But the main concern should be the security of the code you write as that's the most likely place problems will be, its also the only thing you have control of ;)
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Issue with mailing list

Post by Temor »

pasqo83 wrote: What is the security like in php? Sorry sounds quite like a newbie question but I have never understood, is it easy to bypass php or is it quite secure? :D
Php in itself isn't secure at all. So you would have to add all the security yourself if you want any.
You can make a youtube search for SQL-Injection. I do believe Jacek has a tutorial on that, but I'm not entirely sure.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

Temor wrote:Php in itself isn't secure at all.
That's a little misleading, the C code of the php compiler is very secure.

But it does not automatically protect you from things, you need to use the functions available.

I think that's what you meant.
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

jacek wrote:
Temor wrote:Php in itself isn't secure at all.
That's a little misleading, the C code of the php compiler is very secure.

But it does not automatically protect you from things, you need to use the functions available.

I think that's what you meant.

Yeah all I meant was as an overall programming language how secure it is, it is the most common used language in websites as well as javascript (normally working side by side) - there are plenty others but never looked into security aspects of a site so not sure what triggered me to ask.

I just noticed my code has a slight glitch. when you webpage to signup, it automatically starts off with:-

"You have been subscribed, thanks"

then when you fill in the details, you have click the submit button, it looks as if it hasn't gone through but it has and the "You have been subscribed, thanks" doesn't get shown again until you press the submit button the 2nd time round.

Any Ideas?

Again the codes are below? - i am going to retire after this, will do the next part tomorrow and then learn how to create the html coding for the page for the email address as well :D

signup.php
<?php

include('../core/init.inc.php');

if (isset($_POST['firstname'], $_POST['lastname'], $_POST['email'])){
	$errors= array();
	
	if (preg_match('/^[a-z]+$/i', $_POST['firstname']) === 0){
		$errors[] = 'Your first name should contain letters only.';
	}

	if (preg_match('/^[a-z]+$/i', $_POST['lastname']) === 0){
		$errors[] = 'Your last name should contain letters only.';
	}

	if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] = 'The email address you gave does not appear to be calid.';
	}

	if (empty($errors)){
		if (add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']) === false){
		$errors[] = 'You have already subscribed using that email address';
	}

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div>
<?php


if (empty($errors) === false){
	echo '<ul><li>', implode('</li><li>', $errors),'</li></ul>';
		
} else if (isset($errors) === false)
	echo 'Fill in the form to subscribe';
} else{ 
	echo 'You have been subscribed, thanks';
}

?>

Many Thanks
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

It looks like a combination of two typos that would normally give errors has caused this !

Here is your coded indented to fit the blocks you have
<?php

include('../core/init.inc.php');

if (isset($_POST['firstname'], $_POST['lastname'], $_POST['email'])){
	$errors= array();
	
	if (preg_match('/^[a-z]+$/i', $_POST['firstname']) === 0){
		$errors[] = 'Your first name should contain letters only.';
	}

	if (preg_match('/^[a-z]+$/i', $_POST['lastname']) === 0){
		$errors[] = 'Your last name should contain letters only.';
	}

	if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
		$errors[] = 'The email address you gave does not appear to be calid.';
	}

	if (empty($errors)){
		if (add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']) === false){
			$errors[] = 'You have already subscribed using that email address';
		}
	}

	?>

	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Untitled Document</title>
	</head>

	<body>
	<div>
	<?php


	if (empty($errors) === false){
		echo '<ul><li>', implode('</li><li>', $errors),'</li></ul>';
		
	} else if (isset($errors) === false)
		echo 'Fill in the form to subscribe';
} else{ 
	echo 'You have been subscribed, thanks';
}

?>
Never seen that before !

So from your original code, you had
if (add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']) === false){
		$errors[] = 'You have already subscribed using that email address';
Which has a missing ending }

and further down you had
} else if (isset($errors) === false)
	echo 'Fill in the form to subscribe';
which has a missing opening {

And the two kind of cancelled out, and just made the logic wrong.
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

jacek wrote:It looks like a combination of two typos that would normally give errors has caused this !......
And the two kind of cancelled out, and just made the logic wrong.
Thanks, I literally had to go through it all to understand my error before I changed it about :D u learn soo much from reading it.

I am really greatful of you assisting me, i really am loving this forum and hope I can assist helping others as well one day :D
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Issue with mailing list

Post by jacek »

I doubt this same thing would happen more than once !

Anyway, your welcome 8-)
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Issue with mailing list

Post by pasqo83 »

Hey, its me again, I was just finishing off the mailing list, but the tester message is not pulling through on my google and hotmail account, I have left it for 15 minutes as well :( I even tried changing the ' to backticks as well.

Can you shed some light as I am going back and forth on the part 4 tutorial and cannot see any issues with the coding. Thank you in advanced

send_mail.php coding below
<?php

include('../core/init.inc.php');

if (isset($_POST['subject'], $_POST['message'])){
	mail_all($_POST['subject'], $_POST['message'], 'From: admin@chitchatandaway.co.uk');
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="" method="post">

	<label for ="subject">Subject</label><br />
    <input type="text" name="Subject" id="subject" />
    <p></p>

    <label for ="message">Message</label><br />
    <textarea name="message" id="message" rows="15" cols="70"></textarea>
    <p></p>

    <input type="submit" value="send" />

</form>
</body>
</html>
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
Post Reply