Skip to content Skip to sidebar Skip to footer

How To Get The Auto-generated Ids Of The Row Which Is Inserted Into Table?

I am designing Hotel Booking system, where after insertion of the customer information I have to send back conformation page with the booking_id which is auto-generated when custom

Solution 1:

You can use mysql_insert_id() function in PHP to get last inserted ID

mysql_query("INSERT INTO mytable (product) values ('kossu')"); 
printf("Last inserted record has id %d\n", mysql_insert_id());

Solution 2:

get last inserted id with this and use

$last_id = $conn->insert_id;

Solution 3:

you can use mysql_insert_id() for fetching the inserted id

  $result1=mysql_query($query1);
  echo mysql_insert_id()

Solution 4:

Don't echo query result.

use: mysql_insert_id() or mysqli_insert_id() to return the booking ID. Note: while using mysql_insert_id be careful as it's depreciated in php 5.5 and will be removed in future

$id = mysql_insert_id();
echo json_encode($id);

Then in your ajax success function use php file instead if html and pass this variable:

success: function(response){                            
                        window.location.href = "BookingConformation.php?bookingID='+response+'";
                    }

Then in your bookingConfirmation.php file use get method to fetch this variable:

$bookingID=$_GET['bookingID'];
eco $bookinID; //gives your booking id

Post a Comment for "How To Get The Auto-generated Ids Of The Row Which Is Inserted Into Table?"