Jump to content

[SOLVED] MySQL Query Error


pudge1

Recommended Posts

$sql="INSERT INTO Users (userid, Username, Password, EMail, Account Type)

VALUES

(" . $count . "," . $username . "," . $password . "," . $_POST['email'] . "," . $at . ")";

$result = mysql_query($sql , $connection);

 

The problem I am having with the script is that it is not inserting the data. I am 99.99% sure that is the fault of the way I wrote up the $sql= line. Does anyone see what I did wrong?

 

EDIT: I forgot to mention. It IS connecting to the database just fine so that is NOT the problem.

Link to comment
https://forums.phpfreaks.com/topic/179123-solved-mysql-query-error/
Share on other sites

You can't have spaces in column names without denoting it as a name using backticks ( ` ).  (The issue is with the "account type" column).

 

$sql="INSERT INTO Users (userid, Username, Password, EMail, `Account Type`)
VALUES
(" . $count . "," . $username . "," . $password . "," . $_POST['email'] . "," . $at . ")";
$result = mysql_query($sql , $connection);

Warning: 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 '@email.com,User)' at line 3 in

 

That is the error I get.

 

Thanks for whoever posted the new code, still broken though. I just don't see what is wrong I have looked it over a couple times and referred back to the manual.

You have to have quotes in the SQL around the values:

$sql="INSERT INTO Users (userid, Username, Password, EMail, `Account Type`)
VALUES
(" . $count . ",'" . $username . "','" . $password . "','" . $_POST['email'] . "','" . $at . "')";
$result = mysql_query($sql , $connection);

 

I know it's hard to see them but I added them in there.  echo $sql before executing it to see your query.

 

Also, VERY IMPORTANT, do not send user input directly to the database without sanitizing it first!!  Using $_POST['email'] in your query is not a good idea unless you have already done something to prevent SQL injections.  At the very least use mysql_real_escape_string($_POST['email']);.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.