[英]Search function for multiple fields in PHP
I am making a Search function in PHP MySQL. 我正在用PHP MySQL做一个搜索功能。 Currently, my code is working but when trying to search by date, all content appear.
目前,我的代码正在运行,但是当尝试按日期搜索时,会显示所有内容。 It seems that the 2 fields aren't connected.
似乎这两个字段没有连接。
Please help. 请帮忙。 Thanks.
谢谢。
<?php
$query = $_GET['query'];
$date = $_GET['date'];
// gets value sent over search form
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM tblArchive WHERE (Author LIKE '%".$query."%' OR Title LIKE '%".$query."%' or Content LIKE '%".$query."%' AND Date LIKE '%".$date."%')");
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<p><h3>".$results['Title']."</h3>"."<h3>".$results['Author']."</h3>"."<h4>".$results['Date']."</h4>".$results['Content']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No results";
}
?> ?>
$raw_results = mysql_query(
"SELECT * FROM tblArchive
WHERE (Author LIKE '%".$query."%'
OR Title LIKE '%".$query."%' or Content LIKE '%".$query."%')
AND (Date LIKE '%".$date."%')");
Please Try This Query It will work fine. 请尝试此查询,它将正常工作。
$get = 'SELECT * FROM tblArchive';
if(!empty($query)){
$get .= ' WHERE (Author LIKE '%".$query."%' OR Title LIKE '%".$query."%' or Content LIKE '%".$query."%' ';
}
if(!empty($date) && !empty($query)){
$get .= " And Date LIKE '%".$date."%'";
}
else{
$get .= " Where Date LIKE '%".$date."%'";
}
$result = mysql_query($get);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.