Reputation: 8990
im using mysql, these are two tables i have:
posts {id, user_id, post, date}
post_tags {id, tag, post_id(references id in post), user_id}
what im trying to do is if the post has a #tag , i insert the intial post in the POSTS
table, and the data in the post_tags
table, how could i do that simlateanously?
P.S. i already know how to check if a post has a tag!! i just want to undertand, how can insert data into both!! espcially the ID's because they are generated within mysql(autoincrement)!!
Upvotes: 1
Views: 323
Reputation: 9489
You can seperate these two queries and run them after each other. You can use mysql_insert_id() for the last inserted id in an table.
$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
Upvotes: 2