2016-05-17 119 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