douxia9826 2016-07-28 23:22
浏览 45
已采纳

SQL语法错误,我找不到错误[重复]

This question already has an answer here:

I can't find the error after 3 hours trying. Can someone help my ?

this is my query =

$query = "INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid) SELECT * FROM (SELECT '" . $ProductName . "','" . $ProductBarcode . "','" . $ProductPakking . "','" . $ProductPresent . "','" . $ProductMinimuim . "','" . $FotoUrl . "','" . $DBid ."') AS tmp WHERE NOT EXISTS (SELECT name FROM `card` WHERE `ProductName` = " . $ProductName . ",`ProductBarcode` = " . $ProductBarcode . ",`ProductPakking` = " . $ProductPakking . ",`ProductPresent` = " . $ProductPresent . ",`ProductMinimuim` = " . $ProductMinimuim . ",`FotoUrl` = " . $FotoUrl . ",`DBid` = " . $DBid ." ) LIMIT 1;";

This is when I echo my query =

INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
SELECT * FROM (
    SELECT 'brood','8717333541583','zak','-1937','11','img/img.svg','3') AS tmp
WHERE NOT EXISTS (
    SELECT name FROM `card` WHERE
        `ProductName` = 'brood',
        `ProductBarcode` = '8717333541583',
        `ProductPakking` = 'zak',
        `ProductPresent` = '-1937',
        `ProductMinimuim` = '11',
        `FotoUrl` = 'img/img.svg',
        `DBid` = '3' )
) LIMIT 1

Error 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 'ProductBarcode = ,ProductPakking = ,ProductPresent = ,ProductMinimuim = ' at line 1

</div>
  • 写回答

1条回答 默认 最新

  • duanhao5038 2016-07-28 23:25
    关注

    I'm assuming $ProductName is not an integer, so you'd need to include that in quotes here:

    WHERE `ProductName` = '" . $ProductName . "'
    

    Also, please note, you SHOULD NOT be concatenating variables into a string like that. Please see this post for info about how bad this is.

    An updated SQL statement PLEASE DON'T USE THIS, BIND VARIABLES INSTEAD:

    INSERT INTO `card`(ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
    SELECT * FROM (
        SELECT '" . $ProductName . "','" . $ProductBarcode . "','" . $ProductPakking . "','" . $ProductPresent . "','" . $ProductMinimuim . "','" . $FotoUrl . "','" . $DBid ."') AS tmp
    WHERE NOT EXISTS (
        SELECT ProductName FROM `card` WHERE
            `ProductName` = '" . $ProductName . "' AND
            `ProductBarcode` = '" . $ProductBarcode . "' AND
            `ProductPakking` = '" . $ProductPakking . "' AND
            `ProductPresent` = '" . $ProductPresent . "' AND
            `ProductMinimuim` = '" . $ProductMinimuim . "' AND
            `FotoUrl` = '" . $FotoUrl . "' AND
            `DBid` = '" . $DBid ."'
    ) LIMIT 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料