Page 1 of 1

mailingList tutorial

Posted: Mon May 21, 2012 2:38 pm
by constantgamer247
So I'm following the [youtube]http://www.youtube.com/watch?v=dQBFBuYyHVk[/youtube] tutorial and I've run into a bit of a problem. No matter what I do I can not get my signup.php form to insert into the database. I think I'm having a bit of a problem with the
 mysql_select_db();
function as no matter what I include for a parameter I get no errors on the page what so ever T_T

I'm gonig to take an educated guess and say that
mysql_connect('localhost','USERNAME','PASSWORD');
is working as if I change USERNAME and PASSWORD in that line then I get errors such as can't connect to db.

Anything else I should post? My add user function
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;
}
Code for my signup.php form
<?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 only be comprised of letters'; 
	}
	
	if (preg_match('/^[a-z]+$/i', $_POST['lastname'] === 0)){
	$errors[] = 'Your last name should only be comprised of letters'; 
	}
	
	if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
	$errors[] =  'Your email address is not a valid format';
	}	
	
	if (empty($errors)){
		add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']);
	}
	
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1-strick.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>OpenKonga</title>
		<link rel="stylesheet" type="text/css" href="stylez.css" media="all" />
	</head>
	<body>
		<div>
			<?php
			
			//echo print_r($errors);
						
			?>
		</div>
		<div>
			<form action="" method="post">
				<p>
					<label for="firstname">First Name: </label>
					<input type="text" name="firstname" id="firstname" />
				</p>
				<p>
					<label for="lastname">Last Name: </label>
					<input type="text" name="lastname" id="lastname" />
				</p>
				<p>
					<label for="eMail">eMail Address:</label>
					<input type="text" name="eMail" id="eMail" />
				</p>
				<p>
					<input type="submit" value="Signup" />
				</p>
			</form>
		</div>
	</body>
</html>
	
Can't think of anywhere else that the problem is O.o? I've followed the tutorial pretty much verbatim; only exception being I didn't perform the typos (maybe I did and don't know it).

Re: mailingList tutorial

Posted: Mon May 21, 2012 3:12 pm
by bowersbros
its mysql_select_db() for a start, that might have some of the issues in it?

Re: mailingList tutorial

Posted: Mon May 21, 2012 3:58 pm
by constantgamer247
>_< Blast the devil; yah I have
<?php

mysql_connect('localhost','USERNAME','PASSWORD');
mysql_select_db('mailing_list');

$path =  dirname(__FILE__);

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

?>
for my /core/init.inc.php file, I just typed it wrong it my post >_<

So I believe its correct. mysql_select_db(); right?

Re: mailingList tutorial

Posted: Mon May 21, 2012 4:07 pm
by constantgamer247
Okay, does this prove that it is the
mysql_select_db('mailing_list');
is the problem?

I threw in
or die();
statements so now my /core/init.inc.php file looks like
<?php

mysql_connect('localhost','USERNAME','PASSWORD')
or die("Error1");

mysql_select_db('mailing_list')
or die("Error2");

$path =  dirname(__FILE__);

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

?>
And I am getting Error2 ):

if I change line#6 to
mysql_select_db('openkong_mailing_list');
I do not get Error2 but it still also wont insert into the database ):

Could it be that in signup.php form action=""?

Re: mailingList tutorial

Posted: Tue May 22, 2012 6:34 pm
by jacek
In your add_user() function the table and columns names in the SQL should have backticks around them instead of quotes '

Other than that, check your form inputs to make sure they have the correct names I guess.

Re: mailingList tutorial

Posted: Wed May 23, 2012 12:53 am
by constantgamer247
jacek wrote:In your add_user() function the table and columns names in the SQL should have backticks around them instead of quotes '

Other than that, check your form inputs to make sure they have the correct names I guess.
Thank you so much! :)

I thought it was just a different font. Didn't realise that the ` was on the same key with the ~ so I just assumed they were 'singleQuotes'

Anyway I changed it and it now works.

Thank you again :)

Re: mailingList tutorial

Posted: Wed May 23, 2012 11:26 pm
by jacek
The ` tells MySQL that the thing between then is a name of a column, table database etc and the ' tells it that the thing should just be treated as a string.

:)