我已经成功地得到了查询的执行和PDO打印,但我在这里做得不对。这个问题的代码的重要部分在最后几代码块中;为了清晰起见,我将第一部分包括在内。使用PDO查询,没有准备好的语句,多得像来自多种HTML输入字段声明
此代码连接到具有多个输入字段的HTML表单。 PHP通过在WHERE语句中使用AND来附加每个字段的数据来构造一个查询。
这是抛出:我回声$查询变量,我可以看到,查询是否已正确形成,但是当我尝试打印查询结果,没有结果被打印出来。
我按倒在这里使用准备好的声明,并决定尝试让未能建立一个预处理语句具有不同的参数号码后,代码先工作,没有他们。我曾尝试,这个职位的帮助:LIKE query using multiple keywords from search field using PDO prepared statement
所以,撇开准备好的发言的那一刻,谁能告诉我什么,我做错了什么?任何帮助将不胜感激。
<?php
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('titleSearch', 'keywordSearch', 'fullSearch', 'fromYear', 'toYear',
'fromSeconds', 'toSeconds', 'withSound', 'withColor');
$conditions = array();
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition, using a prepared statement
$conditions[] = "$field LIKE CONCAT ('%', $_POST[$field], '%')";
}
}
// build the query
$query = "SELECT keyframeurl, videoid, title, creationyear, sound, color,
duration, genre FROM openvideo ";
// if there are conditions defined, append them to the query
if(count($conditions) > 0) {
$query .= "WHERE " . implode(' AND ', $conditions);
}
//confirm that query formed correctly
echo $query;
//print query results
foreach ($dbh->query($query) as $row){
print $row['videoid'].' - '.$row['title'].'<br />';
}
}
?>
尼斯[SQL注入孔](http://bobby-tables.com)。享受你的服务器pwn3d。 – 2013-04-09 18:51:40
您的格式良好的查询是怎样的? – bestprogrammerintheworld 2013-04-09 19:08:18
http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – jycr753 2013-04-09 19:15:44