PHP,MySql:座位号未正确分配

时间:2018-05-18 20:38:48

标签: php mysql

我已经为我的航空公司预订网站制作了自动分配座位的代码 起初,当我为特定日期预订4人时,它运作良好,即为乘客分配1A,1B,1C,1D。 但是下一次,当我为不同日期的4人预订时,它没有正确分配,即它分配1A,1B,2A,2B而不是1A,1B,1C,1D。

这是我正在使用的一段代码。

<?php
$seat="";  
$s="";
function f1() 
{
  require 'connection.php'; 
$from=$_SESSION['fro']; 
  $to=$_SESSION['to']; 
  $date1=$_SESSION['dep']; 
  $air=$_SESSION['airl']; 
  $count=1; 
$n=array("A", "B", "C", "D");

while($count<=5)    
      {

  for($x=0; $x < count($n); $x++)
    {
  global $seat;
      $check="
SELECT * 
  FROM passenger_records 
 WHERE pas_depdate='$date1' 
   AND pas_air='$air' 
   AND pas_frmcity='$from' 
   AND pas_tocity='$to' 
   AND (pas1_seat='$seat' OR pas2_seat='$seat') 
    OR (pas3_seat='$seat' OR pas4_seat='$seat')";
      $result=$conn->query($check);
        if($result->num_rows > 0)
    {
     $seat=$count.$n[$x];
    }
      }           
$check="
SELECT * 
  FROM passenger_records 
 WHERE pas_depdate='$date1' 
   AND pas_air='$air' 
   AND pas_frmcity='$from' 
   AND pas_tocity='$to' 
   AND (pas1_seat='$seat' OR pas2_seat='$seat') 
   OR (pas3_seat='$seat' OR pas4_seat='$seat')";
            $result=$conn->query($check);
           if($result->num_rows > 0)
            {
             $count++;
            }
           else
             break;
    }
    if($count==6)
    {
      $seat="";
    }
return $seat;
}
    if($cls=="First Class")
    {
       if(!$pas1=="")
          {

       $seat1=f1();
       echo $seat1."<BR>";
   $sq = "UPDATE `passenger_records` SET `pas1_seat` = '{$seat1}' WHERE `passenger_records`.`pas_id` = '$id'";
   $result=$conn->query($sq);

      }
   if(!$pas2=="")
      {

       $seat2=f1();
   $sq = "UPDATE passenger_records SET pas2_seat = '{$seat2}' WHERE passenger_records.pas_id = '$id'";
   $result=$conn->query($sq);
   echo $seat2."<BR>";
      }
   if(!$pas3=="")
      {

       $seat3=f1();
   $sq = "UPDATE passenger_records SET pas3_seat = '{$seat3}' WHERE passenger_records.pas_id = '$id'";
   $result=$conn->query($sq);
   echo $seat3."<BR>";
      }
   if(!$pas4=="")
      {
       $seat4=f1();
   $sq = "UPDATE `passenger_records` SET `pas4_seat` = '{$seat4}' WHERE `passenger_records`.`pas_id` = '$id'";
   $result=$conn->query($sq);
   echo $seat4."<BR>";
      }
}
?>

请帮助并修复错误,因为我需要将项目提交到学校 很快。

1 个答案:

答案 0 :(得分:0)

您检查座位的选择语句不正确。

测试第3和第4个座位的条件是 通过上层“OR”条件

与其他条件分开
SELECT * 
FROM passenger_records 
WHERE pas_depdate='$date1' 
AND pas_air='$air' 
AND pas_frmcity='$from' 
AND pas_tocity='$to' 
AND (pas1_seat='$seat' OR pas2_seat='$seat') 
OR (pas3_seat='$seat' OR pas4_seat='$seat')

使用以下格式替换查询

SELECT * 
FROM passenger_records 
WHERE pas_depdate='$date1' 
AND pas_air='$air' 
AND pas_frmcity='$from' 
AND pas_tocity='$to' 
AND (pas1_seat='$seat' OR pas2_seat='$seat' OR pas3_seat='$seat' OR pas4_seat='$seat')