I have 3 tables. Company, phone, companyPhone.

The company table has an auto_increment companyID primaryKey
The phone table has an auto_increment phoneID primaryKey
The companyPhone table row is made up the companyID, and the phoneID.

On my form the user will enter company information and the company phone number.

After i insert the company row and phone row, how do i get the last ID of those tables to insert a row in my companyPhone table?

I found some documentation on the LAST_INSERT_ID() but I am not sure how to use this in my insert statements.

here are my insert statements for the company table and phone table

insert INTO `COMPANY`(`name`, `website`, `industryTypeID`, `companyStatus`) VALUES (@name, @website, @industryTypeID, @companyStatus);

insert INTO `PHONE`(`number`, `phoneTypeID`,`isDefault`) VALUES (@number, @phoneTypeID, @isDefault
9 Years
Discussion Span
Last Post by Sailor_Jerry

Dear friend

use the below code to multiple table insert

< ?      
      $batchconnection = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
      if ($batchconnection->connect_error) {
          echo "Error Occurred While Connection To DataBase";
      $sqlStatements = "insert into test(name) values('Hitesh');insert into test(name) values('Agrawal');insert into test(name) values('Hello');insert into test(name) values('World')";
 $sqlResult = $batchconnection->multi_query($sqlStatements);
   if($sqlResult == true) {
       echo "Successfully Inserted Records";
   } else {
       echo "Some Error Occured While Inserting Records";

Thanks and Regards


Thanks for the reply. But I need to know how to get the last auto_increment id of my phone and company table, to be able to insert a row into my companyphone table.


To do this you must call select last_insert_id(); after each insert statement, to get the last auto_increment id.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.