gpt4 book ai didi

mysql - 为什么我的数据没有插入到我的数据库中

转载 作者:行者123 更新时间:2023-11-29 16:36:26 27 4
gpt4 key购买 nike

好吧,我对这一切都很陌生,所以如果没有发现“明显”的问题,我很抱歉。我正在尝试使用 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);
});
});

最佳答案

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

关于mysql - 为什么我的数据没有插入到我的数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com