2015-06-05 26 views
-2

我有點堅持使用更新查詢。我已經試過替代語法,但我仍然得到同樣的錯誤您的SQL語法有錯誤;檢查...正確的語法使用近「「類別=

$itemName = mysql_real_escape_string($_POST['itemName']); 
$itemDescription = mysql_real_escape_string($_POST['itemDescription']); 
$itemCategory = mysql_real_escape_string($_POST['itemCategory']); 

if(isset($_POST['itemPrice'])) 
    $itemPrice = mysql_real_escape_string($_POST['itemPrice']); 

$statement = 'update products set "category = ' . 
$itemCategory.', price = "'. 
$itemPrice .'", product = "' . 
$itemName . '", description = "' . 
$itemDescription . '" where id = "' . 
$itemId . '"'; 

if(mysqli_query($db, $statement)) 
{ 
    $kittehHasError = false; 
    $message = $itemName . " has been updated successfully."; 
} 
else 
{ 
    $kittehHasError = true; 
    $message = "Something went wrong: " . mysqli_error($db); 
} 

<p><? echo $message ?></p> 

我收到的錯誤是:

Something went wrong: 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 '"category = games, price = "60.75", product = "Wildstar", description = "Ongoing' at line 1

我在做什麼錯了,我已經設置斷點和所有的值都保持數據和正確的資料,因此我不知道爲什麼查詢是不是?我認爲這可能是因爲描述包含單一e引用,但是,當添加MySQL_real_escape_string()我仍然收到相同的錯誤。

+1

複製結果查詢並直接在SQL客戶端中嘗試它。然後嘗試更改可疑部分,直到它工作。一旦它在那裏工作,你就會知道如何在PHP中構建它。 –

回答

2

變化

$statement = 'update products set "category = ' . 
    $itemCategory.', price = "'. 

$statement = 'update products set category = "'. 
    $itemCategory.'", price = "'. 
+0

對不起,它仍然不起作用;這是我的原始語法,我仍然得到相同的錯誤:'出了點問題:你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在''category =「games」,price =「666.00」,product =「Wildstar」,description =「Ongo'在第1行附近使用正確的語法」 – PIJNSO678

+0

請閱讀我的再次回答,'category'附近的雙引號是錯誤的 –

+0

PHP不輸出我認爲會導致混淆的確切代碼。我在你的答案的下半部分嘗試了你的代碼。仍然導致錯誤。 – PIJNSO678

0

你的查詢有問題。用下面的代碼替換您:

$statement = 'update products set category = "' .$itemCategory.'", 
    price = "'. $itemPrice .'", 
    product = "' . $itemName . '", 
    description = "' . $itemDescription . '" 
    where id = "' . $itemId . '"'; 

category之前有一個不必要的逗號。我認爲它應該在價值部分。

試試上面的代碼。

+0

謝謝,但這是我原來的語法,它仍然導致相同的錯誤:'出錯了:你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在''category =「games」,price =「666.00」,product =「Wildstar」,description =「Ongo'at line 1'附近使用正確的語法。」 – PIJNSO678

+0

@ PIJNSO678在''「category =」gam'之前沒有刪除逗號的錯誤..我想你沒有使用我的代碼,也沒有使用它,你是這麼說的。檢查答案並嘗試。 – FatalError

相關問題