2012-04-24 168 views
0

我有兩個代碼將數據放入數據庫,但它產生錯誤,請查看下面的代碼。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(fnamelnamebirthdaygender)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()); 
+0

你需要引號字段的名稱嗎?它只需要值..是它http://www.w3schools.com/php/php_mysql_insert.asp – zod 2012-04-24 16:05:14

+1

@ user1335825:我希望你插入的值已妥善處理,以防止SQL注入攻擊...... – 2012-04-24 16:06:29

回答

3

mysql_query不能在一個查詢中處理多個語句

docs

mysql_query()發送一個唯一的查詢(多個查詢不支持),以當前活動的數據庫與指定link_identifier

使用mysqli相關(與mysqli_multi_query)如果需要此功能,在服務器上。

+0

謝謝很多Quassnoi,但我仍然會繼續使用MySQL。 – soachishti 2012-04-24 19:10:50