Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 让用户指定要搜索的mysql字段安全吗?_Php_Mysql - Fatal编程技术网

Php 让用户指定要搜索的mysql字段安全吗?

Php 让用户指定要搜索的mysql字段安全吗?,php,mysql,Php,Mysql,我有一个搜索程序,它有多个输入文本框,对应于mysql数据库中的字段。我想知道是否有一个自定义搜索框是安全的,用户可以在其中输入要搜索的实际字段及其值 像这样: <form method='post'> <input type='text' name='param1' /> <input type='text' name='param2' /> <input type='text' name='customField' /> <inp

我有一个搜索程序,它有多个输入文本框,对应于mysql数据库中的字段。我想知道是否有一个自定义搜索框是安全的,用户可以在其中输入要搜索的实际字段及其值

像这样:

<form method='post'>
 <input type='text' name='param1' />
 <input type='text' name='param2' />
 <input type='text' name='customField' />
 <input type='text' name='customValue' />
</form>

这是一个内部网页,实际上只有少数人会看到这些新框,但我想知道这里是否可以进行sql注入之类的操作。

您应该检查它们提供的字段是否位于允许搜索的字段列表/数组中。为了更加安全,在查询中的字段名周围添加反勾号。执行这两项操作将防止通过这些变量进行任何注入。

您应该检查它们提供的字段是否位于允许搜索的字段列表/数组中。为了更加安全,在查询中的字段名周围添加反勾号。做这两件事将防止通过这些变量进行任何注入。

为了安全起见,我会使用PDO,然后确保您相应地处理所有错误。

为了安全起见,我会使用PDO,然后确保您相应地处理所有错误。

Mike首先禁用mysql的多查询模式,其余的都可以


请注意使用单表,如果您不使用连接,那么我不认为会有任何影响。

Mike首先禁用mysql的多查询模式,其余的都可以


请注意使用单表,如果不使用联接,则不会有任何影响。

您必须硬编码所有可能的WARIANT


查看我的答案示例代码:

您必须硬编码所有可能的WARIANT


请参见我的回答示例代码:

PDO(或任何围绕准备好的语句的包装器)不允许绑定列名,只有values.PDO-提供错误的安全感,因为PHP 5.1PDO(或任何围绕准备好的语句的包装器)不允许绑定列名,仅values.PDO-自PHP5.1以来提供了错误的安全感,您认为如何?当然不是!你觉得怎么样?当然不是$白名单=数组('list','of','approved','column','names');如果(在数组($param1,$whitelist)){executequeryhere…}$whitelist=array('list','of','approved','column','names');如果(在数组中($param1,$whitelist)){executequeryhere…}
$param1 = mysql_real_escape_string($_POST['param1']);
$param2 = mysql_real_escape_string($_POST['param2']);
$customField = mysql_real_escape_string($_POST['customField']);
$customValue = mysql_real_escape_string($_POST['customValue']);

$query = "SELECT * FROM mytable WHERE field1 LIKE '" . $param1 . "' AND field2 LIKE '" . $param2 . "' AND " . $customField . " LIKE '" . $customValue . "'";