我目前正在嘗試做一個相當簡單的任務。信息通過「post.php」進行檢索,並在應用程序中插入數據庫(如果ID不存在),或者在ID存在時更新數據。ON DUPLICATE KEY不工作
運行腳本在數據庫中什麼也不寫。我試圖在頁面中打印查詢,並將SQL複製/粘貼到PhpMyAdmin中。它返回
#1064 - 您的SQL語法錯誤;請查看與您的MySQL服務器版本相對應的手冊,以便在正確的語法附近使用 附近'long,description,mobile,type,reported,embed,live,img_url) VALUES('','TE'at line 1
我的代碼是:
$query="INSERT INTO
xxy
(ID
,name
,address
,lat
,long
,description
,mobile
,type
,reported
,embed
,live
,('$ id','$ name','$ address','$ lat','$ lon','$ description','$ mobile','$ type','$ reported',' $''','$ live','$ target_file')重複密鑰更新
name
= VALUES('$ name'),address
= VALUES('$ address'),lat
= VALUES('$ lat'), long
= VALUES('$ lon'),description
= VALUES('$ description'),mobile
= VALUES('$ mobile'),type
= VALUES('$ type'),reported
= VALUES('$ reported'), embed
= VALUES('$ embed'),live
= VALUES('$ live'),img_url
= VALUES('$ img_url')「;
謝謝你的幫助!
更新:
我也嘗試反駁保留字。我目前的SQL代碼仍然拋出一個錯誤。
1064 - 您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,在''$ name'附近使用正確的語法),address = VALUES('$ address'),lat = VALUES('$ lat'),lon = VALUES('$ LON'),德美第2行
我使用更新後的SQL代碼:
INSERT INTO `xxy` (`ID`, `name`, `address`, `lat`, `lon`,
`description`, `mobile`, `type`, `reported`, `embed`, `live`,
`img_url`) VALUES('$id', '$name', '$address', '$lat', '$lon',
'$description', '$mobile', '$type', '$reported', '$embed', '$live',
'$target_file') ON DUPLICATE KEY UPDATE `name`=VALUES('$name'),
`address`=VALUES('$address'), `lat`=VALUES('$lat'),
`lon`=VALUES('$lon'), `description`=VALUES('$description'),
`mobile`=VALUES('$mobile'), `type`=VALUES('$type'),
`reported`=VALUES('$reported'), `embed`=VALUES('$embed'),
`live`=VALUES('$live'), `img_url`=VALUES('$img_url')
你可能會使用一個字符MySQL不同意,是一個撇號。 I.e .:「約翰的咖啡店」,我對嗎?如果是這樣,MySQL正在將其解釋爲「約翰的咖啡店」,導致語法錯誤。你將需要逃避你的數據。 –
我也試着直接在PMA中執行上面的代碼。仍然給出相同的錯誤 –
'在重複密鑰更新名稱='$名稱', 地址='$地址'...' 不需要添加'VALUES' – ojovirtual