title: How to retrieve the latest row from table in php/mysql?
body: <p>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.</p>
  
  <p>The problem is, I do not know to get the current parentBookID to be store in table bookingDetail from table booking.</p>
  
  <pre><code>&lt;?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']) &amp;&amp; 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 "&lt;p&gt;Booth(s): &lt;strong&gt;$allBooth&lt;/strong&gt;&amp;nbsp;&lt;strong&gt;$user&lt;/strong&gt;&amp;nbsp;&lt;strong&gt;$event&lt;/strong&gt;&lt;strong&gt;$userID&lt;/strong&gt;&lt;/p&gt;\r\n";
  
      }
  }
      }
  
      header("refresh:5;url=mybooking.php");
  echo "&lt;img src='loading16.gif' style='margin-top:8px; float:left'/&gt;";
  echo 'You\'ll be redirected in about 5 secs. If not, click &lt;a href="mybooking.php"&gt;here&lt;/a&gt;.';  
  
  }else{
  
  echo "You do not make any booking";
      header("refresh:5;url=booking2.php");
      echo "&lt;img src='loading16.gif' style='margin-top:8px; float:left'/&gt;";
      echo 'You\'ll be redirected in about 5 secs. If not, click &lt;a href="booking2.php"&gt;here&lt;/a&gt;.';   
  }
  
  ?&gt;
  </code></pre>