2016-04-18 81 views
0

這是我的搜索代碼,當我點擊搜索按鈕時,它會給出所有值的輸出,而不是搜索特定的產品標題。搜索不能使用php

任何幫助?

<?php include("config.php"); 
mysqli_select_db("onlinegrocery"); 
$output = ""; 

if(isset($_POST['search'])) { 

$searchq = $_POST['search']; 
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); 

$query = "SELECT * FROM tbl_product WHERE product_title LIKE '%$searchq%'"; 
$sql=mysqli_query($con,$query); 
$count = mysqli_num_rows($sql); 
if($count == 0) { 
    $output = 'Nothing Found'; 
} 
else { 
     while($row = mysqli_fetch_array($sql)){ 
      $product_title = $row['product_title']; 
      $id = $row['product_id']; 

      $output .= '<div> '.$product_title.' </div>'; 
     } 
} 

} 
?> 

<form action="index.php" method="post"> 
           <input type="text" class="form-control" placeholder="Search"> 
           <div class="input-group-btn"> 
            <button class="btn btn-default" type="submit" name="search"><i class="glyphicon glyphicon-search"></i></button> 
           </div> 
          </form> 
         <?php echo("$output");?> 
+0

我認爲' 「#[^ 0-9A-Z] * I」'你不應該從搜索查詢中刪除空格。 '「#[^ 0-9a-z \ s] #i」' – jekaby

回答

1

您在

mysqli_select_db("onlinegrocery"); 

缺少的連接變量,它需要的第一個參數爲您的數據庫連接

這將是

mysqli_select_db($con,"onlinegrocery"); 

閱讀http://php.net/manual/en/mysqli.select-db.php

您的代碼是開放的古老的SQL注入檢查How can I prevent SQL injection in PHP?,以防止它

忘記在搜索name屬性的形式,它會

<form action="index.php" method="post"> 
    <input type="text" class="form-control" placeholder="Search" name="search"> 
    <div class="input-group-btn"> 
     <button class="btn btn-default" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i></button> 
    </div> 
</form> 
+0

你有沒有檢查過? – Saty

+0

謝謝你的答案..但它不工作:( – Peace

+0

你有什麼錯誤嗎? – Saty