...

View Full Version : Resolved registration script syntax eror



jake66
01-04-2012, 05:38 PM
Hi,
I am trying to make a registration script and when I run the code below, I get this 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 'group ) VALUES ( 'theusernameiposted' , '6032e6baa29d1f72eef838fabd7987cbfa3a11f7549d' at line 1
I presume it is just an extra comma or something but I can't spot it!

Any help is greatly appreciated!

Thanks



// my connect and choose database script goes here
$username = $_POST['myusername'];
$password = $_POST['mypassword'];
$group = $_POST['group'];
if(strlen($username) > 30)
header('Location: register.php');
$hash = hash('sha256', $password);
function createSalt()
{
$string = md5(uniqid(rand(), true));
return substr($string, 0, 3);
}
$salt = createSalt();
$hash = hash('sha256', $salt . $hash);
$username = mysql_real_escape_string($username);
$query = "INSERT INTO users ( username, password, salt, group )
VALUES ( '$username' , '$hash' , '$salt' , '$group' )";
mysql_query($query)or die(mysql_error());
mysql_close();
echo "The new user has been created. Thank you.";

Old Pedant
01-04-2012, 08:13 PM
DEBUG DEBUG DEBUG.



echo "<hr>DEBUG SQL: " . $query . "<hr>";

what does that show?

djm0219
01-04-2012, 08:32 PM
group is a reserved word in MySQL which means you'd need to enclose it with back ticks (`) to tell MySQL it's a column name (or choose another name for that column).



$query = "INSERT INTO users ( username, password, salt, `group` )
VALUES ( '$username' , '$hash' , '$salt' , '$group' )";

jake66
01-04-2012, 08:35 PM
group is a reserved word in MySQL which means you'd need to enclose it with back ticks (`) to tell MySQL it's a column name (or choose another name for that column).



$query = "INSERT INTO users ( username, password, salt, `group` )
VALUES ( '$username' , '$hash' , '$salt' , '$group' )";


Thank you very much! Problem solved.
I did not know about reserved words...are there many?

djm0219
01-04-2012, 09:14 PM
Quite a few http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Old Pedant
01-04-2012, 09:23 PM
DOH on me. I saw password there and thought maybe it was reserved, but looked it up and saw it wasn't. Didn't even notice group.

djm0219
01-04-2012, 09:41 PM
I too was a bit surprised that password wasn't a reserved word. The error message he posted gave me the clue about group being the culprit.

jake66
01-04-2012, 10:24 PM
Quite a few http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

crikey, that is a long list!
Thanks very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum