2015-06-05 25 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

相关问题