简体   繁体   English

选择/选项值转换为SQL

[英]Select/Option Value into SQL

I'm what im trying to accomplish is have a select form element with pre-defined options 我正在尝试完成的是使用带有预定义选项的select表单元素

<select name="select1">
   <option value="value1">Value 1</option>
   <option value="value2">Value 2</option>
   <option value="value3">Value 3</option>
</select>

my sql is the following 我的sql是以下

$OptionValue = $_POST['select1'];

is that how i should grab the value of the selected option from a form and put it into a php variable to be queried? 这是我应该如何从表单中获取所选选项的值并将其放入要查询的php变量中?

You need to make sure that you are filtering your user input to prevent against sql injection. 您需要确保正在过滤用户输入,以防止SQL注入。 This alone is quite insecure. 仅此一点是不安全的。

Make sure that you are using either PDO or MySQLi. 确保您使用的是PDO或MySQLi。 And if not using prepared statements, your user input needs to be escaped. 而且,如果不使用准备好的语句,则需要对用户输入进行转义。

See here . 这里

Essentially you have it right though. 从本质上来说,您是正确的。

Edit: 编辑:

From the client side I could easily change it to: 从客户端,我可以轻松地将其更改为:

<select name="select1">
   <option value="value1">Value 1</option>
   <option value="';DROP TABLE users">Value 2</option>
   <option value="value3">Value 3</option>
</select>

or similar... and now you're screwed if you haven't escaped your input. 或类似的东西...现在,如果您没有对输入内容进行转义,那么您将陷入困境。

在执行查询之前$optionValue = mysqli_real_escape_string ($connection , $_POST['select1'])请使用$optionValue = mysqli_real_escape_string ($connection , $_POST['select1']) ,以防止大多数SQL注入。

First step is prevent SQL injections with prepared statements. 第一步是防止使用预准备语句进行SQL注入。 Example: Prepared Statements 示例: 准备好的语句

Use this to put your selected option into query: $mySelectedOption = $_POST['select1']; $query = "SELECT column_name FROM table_name WHERE column_name = '{$mySelectedOption}'"; 使用它可以将您选择的选项放入查询中: $mySelectedOption = $_POST['select1']; $query = "SELECT column_name FROM table_name WHERE column_name = '{$mySelectedOption}'"; $mySelectedOption = $_POST['select1']; $query = "SELECT column_name FROM table_name WHERE column_name = '{$mySelectedOption}'";

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

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