我有兩個代碼將數據放入數據庫,但它產生錯誤,請查看下面的代碼。MYSQL插入語法錯誤
$email = "[email protected]"; //email
$pass = "helloworld"; //password
$fname = "Example"; //first name
$lname = "Man"; //last name
$birth = "2012-2-1"; //birthday
$gender = "male"; //gender
$site_prefix = "my_"; //table prefix
此代碼不工作並輸出一個錯誤
$sql = "
INSERT INTO `{$site_prefix}login` (`email`,`pass`)
VALUES ('$email','$pass');
INSERT INTO `{$site_prefix}users` (`fname`,`lname`,`birthday`,`gender`)
VALUES ('$fname','$lname','$birth','$gender')";
mysql_query($sql,$con) or die(mysql_error());
錯誤 你在你的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的「INSERT INTO my_users(fname
,lname
,birthday
,gender
)VALUES(」示例正確的語法」,‘馬’手冊第2行
此代碼正常工作
$sql = "INSERT INTO `{$site_prefix}login` (`email`,`pass`) VALUES ('$email','$pass');";
$sql1 = "INSERT INTO `{$site_prefix}users` (`fname`,`lname`,`birthday`,`gender`) VALUES ('$fname','$lname','$birth','$gender')";
mysql_query($sql,$con) or die(mysql_error());
mysql_query($sql1,$con) or die(mysql_error());
你需要引號字段的名稱嗎?它只需要值..是它http://www.w3schools.com/php/php_mysql_insert.asp – zod 2012-04-24 16:05:14
@ user1335825:我希望你插入的值已妥善處理,以防止SQL注入攻擊...... – 2012-04-24 16:06:29