2011-03-08 74 views
0

我有两个表我的MySQL数据库,当我添加一个新的岗位(通过PHP)在它创建一个新的行:MySQL查询插入多个表相同的ID

-Post(ID,title,body) 

-pictures(id,id_post,path_picture) 

我怎么能实现通过post ID到相对的pictures id_post

回答

0

无论如何您都不能使用DML插入到两个表中。

回读分配INSERT_ID,执行插入,然后调用mysql_insert_id()

如果你不想从你的PHP代码轮询INSERT_ID,那么你就需要创建一个存储过程。

0

根据您使用的数据库库,可以找出插入行的ID。

例如用PHP提供的mysql库,在插入帖子的查询运行后立即运行mysql_insert_id()。

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

$inserted = mysql_query($insert_sql); 
if ($inserted) 
{ 
    $Post_inserted_id = mysql_insert_id(); 
} 
相关问题