2015-10-12 42 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? –