Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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 SQL WHERE子句_Php_Mysql_Ajax - Fatal编程技术网

安全动态PHP SQL WHERE子句

安全动态PHP SQL WHERE子句,php,mysql,ajax,Php,Mysql,Ajax,什么是安全的方法,例如bindParam,准备插入一个动态where子句。这将通过ajax发送到PHP。所以有些东西来自ajax表单,带有 .php?where=name&what=bob 或许 .php?where=type$what=clothes 然后在PHP中,一切都设置为变量后,例如 if(isset($_POST['where'])){ $where = $_POST['where']; } if(isset($_POST['what'])){ $what= $_

什么是安全的方法,例如bindParam,准备插入一个动态where子句。这将通过ajax发送到PHP。所以有些东西来自ajax表单,带有

.php?where=name&what=bob
或许

.php?where=type$what=clothes
然后在PHP中,一切都设置为变量后,例如

if(isset($_POST['where'])){
  $where = $_POST['where'];
}
if(isset($_POST['what'])){
  $what= $_POST['what'];
}
然后运行一个函数来检索数据

function retrieveData($db, $where, $what){
  $getData = $db->prepare("SELECT name, type, stuff FROM tbl WHERE :where = :what");
  $getData->bindParam(':what',$what);
  $getData->bindParam(':where',$where);
  $getData->execute();
 ..............
}
当我运行这样一个查询时,我总是得到关于

'WHERE name = bob"
所以值被传递了,但是我猜SQL是无效的


谢谢你的帮助。谢谢

我认为应该引用字符串

...WHERE name = 'bob'....
所以试着这样做

 $getData->bindParam(':what',$what,PDO::PARAM_STR, 15);
 $getData->bindParam(':where',$where,PDO::PARAM_STR, 15);

它接受我的:什么,但仍然不是:哪里。也许这是不可能的?没有错误,只是没有结果。例如,当我硬编码名称时会出现一个结果。其中是保留字换行:在backticks中,`您只能将数据作为参数传递,而不能作为标识符I传递。e、 ,数据库,表,列名等等。是的,它几乎可以工作,但它总是告诉我我的列是未知的。所以我想总体答案是,这在安全的情况下是不可能的。@user3585210这是可能的。但是,您应该使用允许列的白名单。好的,谢谢。我使用了一个白名单,它是有效的。