I try to retrieve the latest added bookingID from table booking but I do not know how to get it.
The scenario: when user click ‘submit’ button, mysql query will generate a parentBookID and store userID, eventID in table booking.Then query will get the created parentBookID to insert into table bookingDetail, user will make multiple bookings and these detail will store in table bookingDetail with same parentBookID.
The problem is, I do not know to get the current parentBookID to be store in table bookingDetail from table booking.
<?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> <strong>$user</strong> <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>.';
}
?>
Assuming you have an
AUTO_INCREMENTcolumn inbooking, you could usemysql_insert_id();to get it.