<?php
// open database connection code and then my code as follows
//$id = mysql_insert_id(); << This line wouldn't work ie., $id will be null because there isn't any insert statement before this function call.
//$sql="INSERT INTO clients (clientID, firstname, surname, address1, address2, town, postcode, telephone, email, cardno, expirydate) VALUES ($id,'$_POST[firstname]','$_POST[surname]','$_POST[address1]','$_POST[address2]','$_POST[town]', '$_POST[postcode]','$_POST[telephone]','$_POST[email]','$_POST[cardno]','$_POST[expirydate]')";
//since clientID is an autoincrement field, you don't need to mention it. Use this query instead.
$sql="INSERT INTO clients (firstname, surname, address1, address2, town, postcode, telephone, email, cardno, expirydate) VALUES ('$_POST[firstname]','$_POST[surname]','$_POST[address1]','$_POST[address2]','$_POST[town]', '$_POST[postcode]','$_POST[telephone]','$_POST[email]','$_POST[cardno]','$_POST[expirydate]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$last_insert_client_id = mysql_insert_id();
//get previous insert statement's clientID
//$sql="INSERT INTO bookings (bookingID, clientID, roomID, startdate, enddate, adults, children, roomtype, requirements) VALUES ($id, LAST_INSERT_ID(),'NULL','$_POST[startdate]','$_POST[enddate]','$_POST[adults]','$_POST[children]','$_POST[roomtype]', '$_POST[requirements]')";
$sql="INSERT INTO bookings (clientID, roomID, startdate, enddate, adults, children, roomtype, requirements) VALUES ('$last_insert_client_id','NULL','$_POST[startdate]','$_POST[enddate]','$_POST[adults]','$_POST[children]','$_POST[roomtype]', '$_POST[requirements]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$last_insert_booking_id = mysql_insert_id();
//last insert statement's bookingID
//$result = mysql_query("SELECT * FROM clients WHERE clientID=LAST_INSERT_ID()");
//this also wouldn't work because LAST_INSERT_ID will give you the last inserted id of booking table.. Instead use this
$result = mysql_query("SELECT * FROM clients WHERE clientID='$last_insert_client_id'");
//$result = mysql_query("SELECT * FROM bookings WHERE bookingID=LAST_INSERT_ID()");
//This will work but it will override the value stored in the variable $result. You should use a different variable.
$result1 = mysql_query("SELECT * FROM bookings WHERE bookingID='$last_insert_booking_id'");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>address1</th>
<th>address2</th>
<th>town</th>
<th>postcode</th>
<th>telephone</th>
<th>email</th>
<th>Arrival</th>
<th>Departure</th>
<th>Adults</th>
<th>children</th>
<th>Room Type</th>
<th>Requirements</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['clientID'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" .$row['address1'] . "</td>";
echo "<td>" .$row['address2'] . "</td>";
echo "<td>" .$row['town'] . "</td>";
echo "<td>" .$row['postcode'] . "</td>";
echo "<td>" .$row['telephone'] . "</td>";
echo "<td>" .$row['email'] . "</td>";
}
//considering these are the values returned by 1st query (ie., table client)
while($row = mysql_fetch_array($result1)) {
echo "<td>" .$row['startdate'] . "</td>";
echo "<td>" .$row['enddate'] . "</td>";
echo "<td>" .$row['adults'] . "</td>";
echo "<td>" .$row['children'] . "</td>";
echo "<td>" .$row['roomtype'] . "</td>";
echo "<td>" .$row['requirements'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "Booking is complete";
print "Thank you for booking with us and your booking ID number is ".$last_insert_booking_id;
mysql_close($con);
?>
Try this. :) I hope its clear enough..
Cheers!
P.S. Whenever you are getting the values from the user, you should sanitize the input by using mysql_real_escape_string.
ie., instead of directly using $_POST['name'] , use mysql_real_escape_string($_POST['name']) This will prevent sql injections.