Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数据插入两个表中?_Php_Mysql_Insert - Fatal编程技术网

Php 将数据插入两个表中?

Php 将数据插入两个表中?,php,mysql,insert,Php,Mysql,Insert,我使用mysql,我有两个表: posts {id, user_id, post, date} post_tags {id, tag, post_id(references id in post), user_id} 我想做的是,如果帖子有一个#标记,我在POSTS表中插入初始帖子,然后在post#u标记表中插入数据,我怎么能同时这样做呢 另外,我已经知道如何检查帖子是否有标签了!!我只是想了解一下,如何将数据插入到两者中!!特别是ID,因为它们是在mysql中生成的(自动增量) 您可以将这两

我使用mysql,我有两个表:

posts {id, user_id, post, date}
post_tags {id, tag, post_id(references id in post), user_id}
我想做的是,如果帖子有一个#标记,我在
POSTS
表中插入初始帖子,然后在
post#u标记
表中插入数据,我怎么能同时这样做呢


另外,我已经知道如何检查帖子是否有标签了!!我只是想了解一下,如何将数据插入到两者中!!特别是ID,因为它们是在mysql中生成的(自动增量)

您可以将这两个查询分开,然后依次运行它们。您可以使用mysql\u insert\u id()作为表中最后一个插入的id

$query = "INSERT INTO posts (id, user_id, post, date)
    VALUES (id, user_id, post, date)";
mysql_query($query) or die(mysql_error().$query); // run query

$lastid = mysql_insert_id(); // this will get the last inserted id.

$query = "INSERT INTO post_tags (id, tag, post_id, user_id)
    VALUES (id, tag, ".$lastid.", user_id)";
mysql_query($query) or die(mysql_error().$query); // run query