我尝试从餐桌预订中检索最新添加的bookingID,但我不知道如何获取。
场景:当用户单击'submit'按钮时,mysql查询将生成一个parentBookID并将userID,eventID存储在表预订中,然后查询将获得创建的parentBookID插入到表bookingDetail中,用户将进行多个预订并将这些详细信息存储在表中带有相同parentBookID的表bookingDetail。

问题是,我不知道要从表预订中获取要存储在表BookingDetail中的当前parentBookID。

<?php

session_start();


if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) )  {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
$userID= $_SESSION['AUTHORIZED_CUSTNO'];
$event=$_SESSION['EVENT_ID'];
 include('db.php');

 //check submitted totalDay
 if (isset($_POST['submit']) && isset($_POST['totalDay'])) {


//insert parentBookingID
$parent=mysql_query("INSERT into booking (custNo, eventID,dateBook) VALUES ('$userID','$event',NOW())");

//no logic
$queryParent="SELECT parentBookID from booking where custNo='$userID' and eventID='$event'";
$queryParentResult=mysql_query($queryParent);
while($parentBook = mysql_fetch_array($queryParentResult)){
    $parentBookID=$parentBook['parentBookID'];

$totalDay=$_POST['totalDay'];
$allBooth="";


foreach ($totalDay as $d) {

    echo $d;

    $bookingInfo  = $d;
    $bookingInfo = explode(" ", $bookingInfo);
    echo $bookingInfo[0]; // boothAlias
    echo $bookingInfo[1]; // boothID
    echo $bookingInfo[2]; // day



    $result = mysql_query("SELECT * FROM bookingDetail WHERE boothID='$bookingInfo[1]' and day='$bookingInfo[2]' and username='$user'");

    $num_rows = mysql_num_rows($result);

        if ($num_rows) {
            echo "Exist";
        }else{

    $str = "INSERT INTO bookingDetail (username, custNo, eventID, date, day, boothAlias, boothID, parentBookID) VALUES ('$user', '$userID','$event',NOW(),'$bookingInfo[2]','$bookingInfo[0]','$bookingInfo[1]','$parentBookID');";
    $res = mysql_query($str);

    if($res)
            echo 'Success';
    else
            echo 'Failure';

    $allBooth= substr($allBooth, 0, -2);

echo "<p>Booth(s): <strong>$allBooth</strong>&nbsp;<strong>$user</strong>&nbsp;<strong>$event</strong><strong>$userID</strong></p>\r\n";

    }
}
    }

    header("refresh:5;url=mybooking.php");
echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
echo 'You\'ll be redirected in about 5 secs. If not, click <a href="mybooking.php">here</a>.';

}else{

echo "You do not make any booking";
    header("refresh:5;url=booking2.php");
    echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
    echo 'You\'ll be redirected in about 5 secs. If not, click <a href="booking2.php">here</a>.';
}

?>

最佳答案

假设AUTO_INCREMENT中有一个booking列,则可以使用mysql_insert_id();来获取它。

关于php - 如何从php/mysql中的表中检索最新行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8335948/

10-11 12:42