mysql - 为什么我的数据没有插入到我的数据库中
问题描述
好的,所以我对这一切都很陌生,所以如果没有发现“明显”的问题,我很抱歉。我正在尝试使用 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
其作为 MySQL 语句中字符串的一部分引用conn.query()
。您需要为该行上VALUES (" + itemData.userID + ",
的所有itemData
值连接做。我还建议研究参数化以保护您的应用程序免受 SQL 注入。这是一个简单的例子:如何在nodejs中将参数传递给mysql查询回调
推荐阅读
- node.js - 如何使用 Nodejs 和 Express 隐藏文件的下载路径
- unity3d - 获取 Unity 指南针真北航向
- java - 使用 Spring Data 在数据库中保存大量数据
- xml - Kobo Sqlite 数据库中用于书签的 XML 格式
- node.js - Node.js 仅在重新启动后应用更改
- mysql - SQL 查询以查找具有相同电子邮件域的最大计数
- scala - 将字符串列表转换为案例对象列表
- ios - UIwebview iOS中的异步加载html字符串
- docker - 错误:服务“工作区”未能构建
- java-native-interface - libc:致命信号 11 (SIGSEGV),代码 1,故障地址 0xcdb8007c