Jump to content

Get ID after autoupdate


edynas

Recommended Posts

I got a maybe stupid question

 

I want to insert data into a table. In the table are 3 rows of which one is an id with autoupdate. In the loop where I do the insert I also want to do an other insert but need the value of the id.

 

I have seen several solutions to this but want to make sure I get it right. One way is to count the table and the number of rows should be the number of the id. But I am afraid to use this methode as over time there maybe some deletions of rows and that will effect the number. An other is to call the id value before entering the loop set a variable with the name y (or whatever) with that value and use the ++ at the end of the inner loop to up the id and I saw some bits and pieces of a function that calls the id of the last insert.

 

I need some advice..what is the best way to do this

Link to comment
https://forums.phpfreaks.com/topic/47023-get-id-after-autoupdate/
Share on other sites

Great so the code for it would be something like this

if the columns are id, name, description

 

 

while ($A = mysql_fetch_array($opdracht))
  {
$sql = mysql_query("INSERT INTO cat (`id`, `cat_name`, `cat_desc`) VALUES ('', $cat_name, $cat_desc)" );
$cat_id = mysql_insert_id ( [id] );
$sql = mysql_query("INSERT INTO product2cat (`id`, `cat_id`, `name`, 'descr' ) VALUES ('', $cat_id, $product_name, $desc)" );

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.