【问题标题】:{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax{ 错误:ER_PARSE_ERROR:您的 SQL 语法有错误
【发布时间】:2023-04-07 17:25:01
【问题描述】:

{ 错误:ER_PARSE_ERROR:您的 SQL 语法有错误;查看 与您的 MySQL 服务器版本相对应的手册 在第 1 行的“未定义”附近使用的语法

我尝试在表格列中添加反引号,但没有奏效。

function quantityCheck(ID, itemsPurchased){
    connection.query('Select * FROM `products` WHERE `item_id` ' + ID, function(err, res){
        if (err){console.log(err)};
        if(itemsPurchased <= res[0].stock_quantity){
            var total = res[0].price * itemsPurchased;
            console.log('Your items are available for final purchase.')
            console.log ('Total today for ' + itemsPurchased + ' ' + res[0].product_name + ' is ' + total);

            connection.query('UPDATE products SET stock_quantity = stock_quantity -' + itemsPurchased + 'WHERE item_id = ' + ID);
        } else {
            console.log ('Your purchase could not be processed. We have limited quantities of ' + res[0].product_name);
        }
        display();
    })

我想显示quantityCheck(); connection.query 方法之后的代码。

【问题讨论】:

    标签: mysql syntax


    【解决方案1】:

    您的 SQL 中缺少空格

    改变

    connection.query('UPDATE products SET stock_quantity = stock_quantity -' + itemsPurchased + 'WHERE item_id = ' + ID);
    

    connection.query('UPDATE products SET stock_quantity = stock_quantity -' + itemsPurchased + ' WHERE item_id = ' + ID);
    

    注意我在 WHERE 之前插入了一个额外的空格

    另外,考虑使用准备好的语句来避免 SQL 注入攻击

    【讨论】:

      【解决方案2】:

      这主要是由于update 语句中的适当间距。应该像下面这样。同样,您的查询目前容易受到 Sql Injection 攻击。改用参数化查询

      connection.query('UPDATE products SET stock_quantity = stock_quantity - ' + itemsPurchased + ' WHERE item_id = ' + ID);
      

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 2019-08-29
        • 2019-10-25
        • 1970-01-01
        • 2019-05-08
        • 2020-01-18
        • 1970-01-01
        • 2020-12-27
        • 1970-01-01
        相关资源
        最近更新 更多