2015-10-12 85 views
1

我需要從指定的表中選擇數據,並從顯示的數據中選擇一個變量,然後用它從另一個表中選擇並顯示所選數據,但是當數據即從預訂表中選擇的是多隻顯示變量中的第一個數據,這裏是我的代碼:如何在php中循環

$res1=mysqli_query($bd,"select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'"); 
$num1=mysqli_num_rows($res1); 
if($num1>0) 
{ 
    for($y=0;$y<$row1=mysqli_fetch_assoc($res1);$y++) 
    { 
     $res=mysqli_query($bd,"select * from rooms where capacity>='$newcap' and room_number!='".$row1['roomnumber']."'"); 
     while($row=mysqli_fetch_assoc($res)) 
     { 
      echo'<div class="col-lg-4 col-md-4 col-sm-12">'; 
       echo'<div class="newsBox"> 
        <div class="thumbnail"> 
         <figure><img src="reservation/img/rooms/'.$row['img'].'" width="230" height="150"></figure> 
         <div class="caption maxheight2"> 
         <div class="box_inner"> 
            <div class="box"> 
             <a class="title"><strong>'.$row['name'].'</strong></p> 
             <b>'.$row['description'].'</b> 
             <p>'.$row['price'].'</p> 
            </div> 
            <a class="btn btn-default" href="info_pay.php?roomnumber='.$row['room_number'].'&roomtype='.$row['name'].'&from='.$_POST['from'].'&adult='.$_POST['adult'].'&child='.$_POST['child'].'&to='.$_POST['to'].'&roomprice='.$row['price'].'"><span class="glyphicon glyphicon-plus">Select this Room</span></a> 
          </div> 
         </div> 
        </div> 
       </div>'; 
      echo'</div>'; 
     } 
    } 
} 
+2

[你的腳本是在對SQL注入攻擊的風險。(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

+0

對不起IM只是一個初學者可以幫我用我的代碼? – user3425772

+0

您應該解釋您希望代碼執行的操作,即粘貼的代碼無法實現。 – sunny

回答

-1

你可能想嘗試使用heredoc不同的方法,因爲它是不容易報價錯誤的,這裏有一個使用heredoc循環查詢mysqli的完整示例。

<?php 

$con=mysqli_connect("localhost","my_user","my_password","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql="select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'"; 

if ($result=mysqli_query($con,$sql)) 
    { 
    while ($row=mysqli_fetch_row($result)) 
    { 
echo <<< LOL 
     <div class="col-lg-4 col-md-4 col-sm-12"> 
       <div class="newsBox"> 
        <div class="thumbnail"> 
         <figure><img src="reservation/img/rooms/{$row['img']}" width="230" height="150"></figure> 
         <div class="caption maxheight2"> 
         <div class="box_inner"> 
            <div class="box"> 
             <a class="title"><strong>{$row['name']}</strong></p> 
             <b>{$row['description']}</b> 
             <p>{$row['price']}</p> 
            </div> 
            <a class="btn btn-default" href="info_pay.php?roomnumber={$row['room_number']}&roomtype={$row['name']}&from={$_POST['from']}&adult={$_POST['adult']}&child={$_POST['child']}&to={$_POST['to']}&roomprice={$row['price']}"><span class="glyphicon glyphicon-plus">Select this Room</span></a> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
LOL; 

    // Free result set 
    mysqli_free_result($result); 
} 

} 
//close mysqli connection 
mysqli_close($con); 
?> 
+0

小心評論downvote? –