php,mysql:座位号分配不正确

50few1ms  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(406)

我已经为我的机票预订网站自动分配座位的代码。
首先,当我为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>";
      }
}
?>

请帮助和纠正错误,因为我需要提交项目到学校很快。

jq6vz3qz

jq6vz3qz1#

您检查座位的select语句不正确。
测试第3和第4个座椅位置的条件与其他条件用一个更高级别的“或”条件分隔

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')

相关问题