2016-04-18 14 views
0

Das ist mein Suchcode, wenn ich auf die Suchschaltfläche klicke, gibt es die Ausgabe aller Werte, anstatt den bestimmten Produkttitel zu suchen.Suche funktioniert nicht mit PHP

Irgendwelche Hilfe ??

<?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

ich denke, ' "# [^ 0-9a-z] #i" sein' Sie sollten keine Leerzeichen aus der Suchanfrage entfernen. '" # [^ 0-9a-z \ s] #i "' – jekaby

Antwort

1

Sie fehlende Verbindungsvariable bei

mysqli_select_db("onlinegrocery"); 

Es müssen erste Parameter als Datenbankverbindung

Es

wäre
mysqli_select_db($con,"onlinegrocery"); 

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

Ihr Code ist offen für ehrwürdige SQL-Injection-Check How can I prevent SQL injection in PHP? Vergiss es auf der Suche nach Namen Attribut

zu verhindern bilden würde es

<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

Hast du das überprüft ?? – Saty

+0

Vielen Dank für Ihre answer..but seine nicht funktioniert :( – Peace

+0

Haben Sie einen Fehler bekommen ?? – Saty