我的SQL查询中的语法错误(ON DUPLICATE KEY UPDATE)

时间:2017-02-14 09:26:01

标签: php mysql

我的mysql查询有问题,我尝试执行ON DUPLICATE KEY UPDATE,但是我得到错误,我不知道为什么,也许是因为我对我的更新语句做错了。这是我的sql代码:

$q3='INSERT INTO prestashop.ps_product_supplier (product_supplier_reference,product_supplier_url) values ("%s","%s") WHERE id_product=%d ON DUPLICATE KEY UPDATE prestashop.ps_product_supplier SET product_supplier_reference= "%s", product_supplier_url= "%s" WHERE id_product = %d';

$sql3 = sprintf($q3, $_REQUEST['tki'],$_REQUEST['tki2'], $_REQUEST['id']);

我知道使用$ sql3这样的不安全方法!但我用它来满足我的个人需求

3 个答案:

答案 0 :(得分:0)

$q3='INSERT INTO prestashop.ps_product_supplier (id_product,product_supplier_reference,product_supplier_url) values (%d,"%s","%s") ON DUPLICATE KEY UPDATE prestashop.ps_product_supplier SET product_supplier_reference= "%s", product_supplier_url= "%s" WHERE id_product=%d';

$sql3 = sprintf($q3, $_REQUEST['tki'],$_REQUEST['tki2'], $_REQUEST['id']);

答案 1 :(得分:0)

$q3='INSERT INTO prestashop.ps_product_supplier (product_supplier_reference,product_supplier_url) values ("%s","%s") WHERE id_product=%d ON DUPLICATE KEY UPDATE prestashop.ps_product_supplier SET product_supplier_reference= "%s", product_supplier_url= "%s"';

$sql3 = sprintf($q3, $_REQUEST['tki'],$_REQUEST['tki2'], $_REQUEST['id'],$_REQUEST['tki'],$_REQUEST['tki2']);

答案 2 :(得分:0)

在您的情况下,使用REPLACE语句会更容易 https://dev.mysql.com/doc/refman/5.7/en/replace.html

$q3='REPLACE INTO prestashop.ps_product_supplier (id_product,product_supplier_reference,product_supplier_url) values (%d,"%s","%s")';
$sql3 = sprintf($q3,$_REQUEST['id'], $_REQUEST['tki'],$_REQUEST['tki2']);

您的查询中的错误也是您在INSERT语句中使用WHERE子句 https://dev.mysql.com/doc/refman/5.7/en/insert.html

$q3='INSERT INTO prestashop.ps_product_supplier (id_product,product_supplier_reference,product_supplier_url) values (%d,"%s","%s") ON DUPLICATE KEY UPDATE product_supplier_reference= "%s", product_supplier_url= "%s"';
$sql3 = sprintf($q3,$_REQUEST['id'], $_REQUEST['tki'],$_REQUEST['tki2'],$_REQUEST['tki'],$_REQUEST['tki2']);