首页 > 解决方案 > 为什么我的数据没有插入到我的数据库中

问题描述

好的,所以我对这一切都很陌生,所以如果没有发现“明显”的问题,我很抱歉。我正在尝试使用 POSTMAN 发布数据,但我不断收到此错误。有谁知道问题是什么?

{

    "error": {
        "code": "ER_PARSE_ERROR",
        "errno": 1064,
        "sqlMessage": "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 'itemData.GPU\", \"itemData.storage\", \"itemData.size\", \"itemData.price\")' at line 1",
        "sqlState": "42000",
        "index": 0,
        "sql": "INSERT INTO orderdetails (userID, title, device, type, CPU, RAM, GPU, storage, size, price) VALUES (\"itemdata.userID\", \"itemData.title\", \"itemData.device\", \"itemData.type\", \"itemData.CPU\", \"itemData.RAM, \"itemData.GPU\", \"itemData.storage\", \"itemData.size\", \"itemData.price\");"
    }
}

我试图发布的数据:

{
    "userID":1,
    "title":"Nitro 5 Spin Laptop | NP515-51 | Black",
    "device":"laptop",
    "type":"Convertible",
    "CPU":"i5-8250U",
    "RAM":"8 GB",
    "GPU":"GTX 1050",
    "storage":"SSD",
    "size":"256 GB",
    "price":1099
}

.

 module.exports.add = function(conData, itemData, callback){
        db.connect(conData, function(err, conn){

            if (err) {
                callback(err);
                return;
            }

            conn.query("INSERT INTO orderdetails (userID, title, device, type, CPU, RAM, GPU, storage, size, price) VALUES (\"itemdata.userID\", \"itemData.title\", \"itemData.device\", \"itemData.type\", \"itemData.CPU\", \"itemData.RAM, \"itemData.GPU\", \"itemData.storage\", \"itemData.size\", \"itemData.price\");", itemData, function(err,result){

                callback(err, result);
            });
        });

标签: mysqlexpresspostpostman

解决方案


您不能将itemData其作为 MySQL 语句中字符串的一部分引用conn.query()。您需要为该行上VALUES (" + itemData.userID + ",的所有itemData值连接做。我还建议研究参数化以保护您的应用程序免受 SQL 注入。这是一个简单的例子:如何在nodejs中将参数传递给mysql查询回调


推荐阅读