tags:

views:

67

answers:

4

There's gotta be something small I keep missing here, but I can't find it for the life of me.

$insert = mysql_query("INSERT INTO USERS 
(`FBID`, `FIRST_NAME`, `LAST_NAME`, `GENDER`) 
VALUES ('$fbid', '$firstName', '$lastName', '$gender')");

The error is:

Error: 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 '1' at line 1

Any ideas?

A: 

Do any of your names contain single quotes?

Try writing out the value of the query to log/console/debug to ensure that it's what you expect.

p.campbell
+2  A: 

You are not having variables correctly escaped. Use mysql_real_escape_string and code like this:

$insert = mysql_query("INSERT INTO USERS (`FBID`, `FIRST_NAME`, `LAST_NAME`, `GENDER`)
   VALUES (
            '".mysql_real_escape_string($fbid)."',
            '".mysql_real_escape_string($firstName)."',
            '".mysql_real_escape_string($lastName)."',
            '".mysql_real_escape_string($gender)."'
          )");

If the variables contain any quotes, they create the problem if you don't properly escape them.

shamittomar
Thanks for the suggestions. I've tried everything, but keep getting the error. Although, the query executes correctly and other than the error message, there are no problems.
SteveMack
@Steve: if your query executes correctly, data is inserted but MySQL still complains about a syntax error, then the syntax error must be on another query.
BoltClock
A: 

Try wrapping your variables in {}.

'{$fbid}', '{$firstName}', '{$lastName}', '{$gender}'

Otherwise you are going to have to use string concatenation. '".$fbid."','".$firstName."','"...

I'm assuming your variables already contain proper escaped data.

Brent Baisley
He's building a double-quoted string. Variables will interpolate just fine into that. You only need the braces if you have text immediately after a variable that could get confused with the variable `$x = 1; $y = "$x23"` will not give you `123`, or if you need to access extra levels of a multidimensional array within the string. `$x[0][1]` is interpreted as $x[0] . '[1]'.
Marc B
A: 

Try doing it like this:

$sql = <<EOL
INSERT INTO USERS (`FBID`, `FIRST_NAME`, `LAST_NAME`, `GENDER`) 
VALUES ('$fbid', '$firstName', '$lastName', '$gender')
EOL;
$stmt = mysql_query($sql) or die("MySQL error: " . mysql_error());

This will preserve the query for you in $sql so you can echo it out elsewhere and see what was actually produced.

Marc B