简体   繁体   中英

Entering Email Addresses into MYSQL Database

I am trying to insert emails into a MYSQL table, and I am getting an 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 ' 19, 'blah@gmail.com')' at line 1

I've looked over the internet, and tried different combinations of collations and codes, but nothing will work. Is the '@' the problem here? I am getting this email address from decoding Facebook's JSON user object. Here are snippets from my code:

$user = json_decode(file_get_contents($jsonurl));
$userid = $user->id;
if($user->gender == "male") $usergender = TRUE;
else $usergender = FALSE;
$useremail = $user->email;

mysql_select_db("kirkstat", $con);
$result = mysql_query("INSERT INTO table (id, access, gender, age, email) VALUES ($userid, '$access_token', $usergender, 19, '$useremail')");
if (!$result){
    echo("error.\n");
    die('Invalid query: ' . mysql_error());
}

id is a bigint, access is a varchar, gender is a binary, age is an int, and email is a varchar.

Thanks for your help!

false casts as a string to an empty string. An empty string is not valid in SQL for an integer column (or a column value of any kind since it won't have a '' either).

Instead of false/true use 0/1.

@ signs are fine in text. I believe it's your "gender" value that's probably causing the error -- you should echo out the full query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM