简体   繁体   English

使用准备好的语句插入选择 MySQL

[英]Insert select MySQL with prepared statements

I am wondering if I need to do this.我想知道我是否需要这样做。

To make it more secure, all the things inserted into database is selected from another table with specific clause that is posted from the user.为了使其更安全,所有插入数据库的内容都是从用户发布的具有特定子句的另一个表中选择的。

I use the id for the identity:我使用id作为身份:

$identity = $_POST['id'];

$stmt = $mysqli->prepare ("INSERT into table_one (col_1, col_2, col_3)
         VALUES (?,?,?)");

//This is what I use to do
$stmt >bind_param ("sss", $valua, $valueb, $valuec);

//But now I want to that like this
$stmt >bind_param ("sss", SELECT valuea, valueb, valuec FROM ANOTHERtable WHERE id = $identity);

$list->execute();
$list->close();

Is it possible?是否可以? And how is the correct way to do this?这样做的正确方法是什么?

You dont need to bind the values from your other table.您不需要绑定其他表中的值。 You just need to prepare those for the values that the user provides.您只需要为用户提供的值准备那些。 You can safely use the existing values.您可以安全地使用现有值。

$stmt = $mysqli->prepare ("INSERT into table_one (col_1, col_2, col_3)
        SELECT valuea, valueb, valuec FROM ANOTHERtable WHERE id = ?");
$stmt >bind_param ("i", $identity);
$stmt->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM