2014-04-20 66 views
-4

我一直試圖插入表格中的數據在兩張表中,但我沒有得到幸運,最後我嘗試了第一張表插入,但第二張表是空的,沒有什麼插入它,下面是我的查詢代碼:我想能夠從兩個表中的表格插入數據

$query1=mysql_query("INSERT INTO $Tname (tnumber2, results, idate, iaddress, tstatus, saddress, scountry, ddate, daddress) VALUES('$tnumber2','$results','$idate','$iaddress','$tstatus','$saddress','$scountry','$ddate','$daddress')"); 
$id = mysql_insert_id(); 
$query2=mysql_query("INSERT INTO $UPname (tnumber2, pdate, act, paddress, up) VALUES('$tnumber2','$pdate','$act','$paddress','$up')"); 

我收到此錯誤:

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 'update (tnumber2, pdate, act, paddress, up) VALUES('59644039299744','59644039299' at line 1

+0

您從第一個查詢中獲得'$ id',但在第二個查詢中沒有做任何處理。我建議將調用添加到'mysql_error()'來查看是否有任何錯誤被數據庫返回。 – andrewsi

+0

這裏是錯誤...........你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'update(tnumber2,pdate,act,paddress,up)附近使用正確的語法。VALUES('59644039299744','59644039299'at line 1 – Wilson

回答

1

從錯誤:

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 'update (...

它看起來像你的表叫update

這是SQL中的保留字,所以它會產生一個錯誤。

如果可能,您應該重命名該表,因爲使用保留字作爲表或列名稱會造成混淆;但如果這不可行,你可以用反引號來避開這個名字;試試這個作爲你的第二個SQL調用:

INSERT INTO `$UPname` (tnumber2, pdate, act, paddress, up) VALUES('$tnumber2','$pdate','$act','$paddress','$up')"); 

我還將添加強制性警告mysql_功能過時,你應該切換到mysqli_或PDO - 他們都幫助你編寫更安全的代碼。另外,如果你要使用表名變量,你需要確保你非常非常好地驗證它們。

+0

非常感謝我得到了什麼現在尋找 – Wilson

相關問題