0

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 ?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.