我的网页中有三个文本框,使用html5 datalist标记自动完成。现在,我想在用户点击搜索按钮时显示搜索结果。
如何获取所有三个文本框字段值并在一个sql查询中搜索以在另一个页面中取消相似的结果..
HTML:
<form class="tg-formtheme tg-formsearch">
<fieldset>
<legend>Search Thousands of verified bussiness</legend>
<div class="form-group">
<input type="text" name="keyword" class="form-control" list="city" placeholder="Enter city">
</div>
<datalist id="city">
<option value="Bangalore">
<option value="hyderabad">
<option value="mumbai">
<option value="pune">
<option value="chennai">
<option value="kolkata">
<option value="jaipur">
<option value="delhi">
<option value="kanput">
<option value="indore">
<option value="patna">
<option value="mysore">
</datalist>
<div class="form-group">
<input type="text" name="keyword" class="form-control" list="location" placeholder="Enter location">
</div>
<datalist id="location">
<option value="Marathahalli">
<option value="BTM Layout">
<option value="HSR Layout">
<option value="Banashankari">
<option value="Basavanagudi">
<option value="Domlur">
<option value="Jayanagar">
<option value="Koramangala">
<option value="Rajajinagar">
<option value="Sadashivanagar">
<option value="Nagarbhavi">
<option value="Whitefield">
</datalist>
<div class="form-group">
<input type="text" name="keyword" class="form-control" placeholder="select Category">
</div>
<div class="tg-btns">
<button class="tg-btn" type="submit">Search Now</button>
<button class="tg-btn" type="submit">Reset all</button>
</div>
</fieldset>
</form>
PHP
if(isset($_POST['search'])){
$city = trim($_POST['city']);
$location = trim($_POST['location']);
$category = trim($_POST['category']);
$stmt = $search->runQuery("SELECT * FROM jposts WHERE city LIKE %'.$city.'% OR location LIKE %'.$location.'% OR business_name LIKE %'.$category.'%");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
echo $row['city'] . "<br>";
echo $row['business_name'];
echo $row['address'];
}
提交数据后,我收到以下错误:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%'. tirupati .'% OR location LIKE %'. amnagar .'% OR business_name LIKE %'. maci' at line 1 in C:\xampp\htdocs\justshow.in\index.php:20 Stack trace: #0 C:\xampp\htdocs\justshow.in\index.php(20): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\justshow.in\index.php on line 20
答案 0 :(得分:0)
使用LIKE
- 运算符在查询中出现语法错误。你必须在引号中使用precent-char,如下所示:
"... WHERE city LIKE '%".$city."' OR ...
最终的查询应如下所示:
... WHERE city LIKE '%mumbai' OR ...
查看this链接,了解有关LIKE的更多信息。
答案 1 :(得分:0)
您的刻度线未正确放置。试试这个:
$stmt = $search->runQuery("SELECT * FROM jposts WHERE city LIKE '%".$city."%'
OR location LIKE '%".$location."%' OR business_name LIKE
'%".$category."%'");