简体   繁体   中英

Mysql query insert multiple table same id

I have two table in my mysql database and when I add a new post (by PHP) a new row is created in it:

-Post(ID,title,body)

-pictures(id,id_post,path_picture)

How could I achieve to pass the post ID to the relative pictures id_post ?

After you insert the post you can call last_insert_id to get the ID the post got. See http://php.net/manual/en/function.mysql-insert-id.php

You can't insert into two tables using DML anyway.

To readback the insert_id assigned, perform the insert then call mysql_insert_id()

If you don't want to poll the insert_id from your PHP code, then you'll need to create a stored procedure.

Depending on what database library you use you can find out the id of the inserted row.

For example with the mysql library that PHP provides you run mysql_insert_id() right after the query to insert the post is run.

http://php.net/manual/en/function.mysql-insert-id.php

$inserted = mysql_query( $insert_sql );
if ( $inserted )
{
     $Post_inserted_id = mysql_insert_id();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM