2016-05-17 117 views
1

我有一個問題讓我的查詢從我的數據庫中獲取所有需要的結果。 我在我的主頁上進行搜索,該主頁將用戶引導至我正在討論的頁面。用戶使用他們的郵政編碼在他們的地區搜索餐館。PHP:雖然循環不顯示分貝問題的所有結果

我的查詢檢查數據庫的結果匹配什麼已經輸入到搜索欄,我的問題是它只顯示1結果,即使他們不止一個。它總是顯示輸入到數據庫中的最後一家餐廳。

我似乎無法看到問題所在,任何幫助或建議將不勝感激。

$output=''; 
    if(isset($_POST['search'])){ 
$searchq= $_POST['search']; 


      $sql=mysqli_query($dbc,"SELECT * 
FROM Rest_Details 
WHERE Delivery_Pcode.Pcode LIKE '%".$searchq."'") or die ("could not search!"); 
      echo var_dump($sql); 

      $count = mysqli_num_rows($sql); 
      if($count ===0){ 
$output ='<b>Oh no! We do not currently deliver to '.$searchq.'</b></br> 
        Please leave your email address & we will notify you as soon as we do! </br> 
<form action="Area_Rest_page.php"> 
<input type="text" name="FirstName" value=""> 
<input type="submit" value="Submit"> 
</form>'; 

      }else{ 
       $i=1; 
      }while($row_prods= mysqli_fetch_array($sql)){ 
      $EXAMPLE = $row_prods['EXAMPLE']; 


      $output ='<h3 id="rest_name">'.$rest_name .'</h3>'. 

        ;  
      $i++; 

然後,下面的內容放置在頁面的下方。

print("$output"); 

的DB和連接成功,會議已經開始,我也有錯誤處理程序(沒有錯誤)

+1

** WARNING **:當使用'mysqli'你應該使用[參數化查詢(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[ 'bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **絕不**將'$ _POST'或'$ _GET'數據直接放入查詢中,如果有人試圖利用您的錯誤,這會非常有害。 – tadman

回答

2

同德爾建議,但你不能使用(+)號進行連結,使用(。 )代替。

$output = $output . '<h3 id="rest_name">'.$rest_name .'</h3>'. 
        '<p class="title"> Price</p>'. 
        '<p class="title">Avg Delivery</p>'. 
        '<p class="title">Cost</p>'. 
        '<a href="product_page.php?rest_id='.$rest_id.'&Postcode='.$searchq.'"><button id="feed">Feed Me!</button></a>'. 
        ; 
+0

好吧!學到了新東西。謝謝 – Blue