将字段值注入节点中的 sql 查询
Injecting field values into sql queries in node
我正在 运行 在节点中执行 sql 查询:
connection.query(`update foodplan set ? = ? where id = ?;`, [meal, favouriteChoice, idValue],
function (err) {
if (err) throw err;
else console.log('Successful update');
});
膳食价值可以是 'breakfast'、'lunch' 或 'dinner' - 这些是 foodplan table 中的 3 个字段值。但是,当这是 运行 时,我收到以下错误:
"Error: 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 ''dinner' = 'wraps' where id = '24'' at line 1"
有人知道怎么回事吗?
查询参数不仅仅是进行字符串替换的另一种方式。参数占位符 ?
保留为占位符,直到 SQL 被解析。 SQL 引擎只记得,"oh, there's going to be a value supplied here."
这就是它作为 SQL 注入的修复程序的原因——无论您最终将什么作为参数值传递,SQL 引擎都不会将其视为单一值.
不是列名,不是任何其他类型的标识符,不是表达式,不是 SQL 关键字等。
但由于这个原因,您不能使用参数代替列名。仅代替单个值。
您必须拼出所有 SQL 语法、表达式、标识符等。在 解析 SQL 之前。否则,解析器无法验证您的语法,无法确认具有该名称的列存在,以及您是否有权查询该列。
抱歉,您必须将列名放入查询字符串中,不能将其作为参数。
所以你问:
is there any way to make this work without having to write separate queries to insert into breakfast, lunch and dinner?
使用字符串连接形成 SQL 查询是安全的 如果 字符串是安全的。在这种情况下,您可以确保将列名 breakfast、lunch 和 dinner 列入白名单并在您的代码控制之下,并且不允许使用任何不受信任的输入。
我正在 运行 在节点中执行 sql 查询:
connection.query(`update foodplan set ? = ? where id = ?;`, [meal, favouriteChoice, idValue],
function (err) {
if (err) throw err;
else console.log('Successful update');
});
膳食价值可以是 'breakfast'、'lunch' 或 'dinner' - 这些是 foodplan table 中的 3 个字段值。但是,当这是 运行 时,我收到以下错误:
"Error: 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 ''dinner' = 'wraps' where id = '24'' at line 1"
有人知道怎么回事吗?
查询参数不仅仅是进行字符串替换的另一种方式。参数占位符 ?
保留为占位符,直到 SQL 被解析。 SQL 引擎只记得,"oh, there's going to be a value supplied here."
这就是它作为 SQL 注入的修复程序的原因——无论您最终将什么作为参数值传递,SQL 引擎都不会将其视为单一值.
不是列名,不是任何其他类型的标识符,不是表达式,不是 SQL 关键字等。
但由于这个原因,您不能使用参数代替列名。仅代替单个值。
您必须拼出所有 SQL 语法、表达式、标识符等。在 解析 SQL 之前。否则,解析器无法验证您的语法,无法确认具有该名称的列存在,以及您是否有权查询该列。
抱歉,您必须将列名放入查询字符串中,不能将其作为参数。
所以你问:
is there any way to make this work without having to write separate queries to insert into breakfast, lunch and dinner?
使用字符串连接形成 SQL 查询是安全的 如果 字符串是安全的。在这种情况下,您可以确保将列名 breakfast、lunch 和 dinner 列入白名单并在您的代码控制之下,并且不允许使用任何不受信任的输入。