为什么不将我的数据插入数据库

时间:2018-12-02 10:55:41

标签: mysql express post postman

好的,所以我对这一切都是新手,所以如果找不到“明显”的问题,我感到非常抱歉。我正在尝试使用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);
            });
        });

1 个答案:

答案 0 :(得分:0)

您不能在itemData的MySQL语句中将conn.query()引用为字符串的一部分。您需要串联对该行上所有VALUES (" + itemData.userID + ",的值进行itemData的连接。我还建议研究参数化,以保护您的应用程序免受SQL注入。这是一个简单的示例:How to pass parameters to mysql query callback in nodejs