Akira Dawson
Akira Dawson

Reputation: 1267

Get results from last executed mysqli query in php and use the results in another query

I have some AJAX/PHP/SQL stuff happening at the moment.

On one page, you select people you want to put into the database (with checkboxes) jQuery collects the values assigned to the checkbox and stores it in an array. Then I use AJAX to to send the array to a PHP file.

In the PHP file, I want to use that array to insert the value into a table. Which works great.

Once the query is executed, I want to be able to grab the info stored, specifically the ID that is generated to use in another query. I just don't know how to call the last executed query and grab the ID value. This is what I have so far in my PHP file:

$myArray = $_REQUEST['activitiesArray'];
mysqli_query($conn,"INSERT INTO Groups (leader_ID) VALUES (".$myArray[0].")");
printf("%s\n", mysqli_info($conn)); //debugging, see if I can pull out anything.

note - This code is very basic. I just want functionality to work before making it dynamic

In the Groups table there are 2 columns, leader_ID and gp_ID (which auto increments). I want to use the last executed query to find out what the value of that gp_ID is so I can use it to update another table. I don't know if this is at all possible. Any insight would be fantastic, cheers.

Upvotes: 0

Views: 568

Answers (1)

Barmar
Barmar

Reputation: 782693

use mysqli_insert_id($conn) to get the auto-increment ID created by the last INSERT query on the connection.

Upvotes: 1

Related Questions