kaiman Posted January 13, 2011 Share Posted January 13, 2011 I have two tables. The first with an auto-increment field of id and the second with a article_id field. I want to get the value of the auto-increment field in the first table and insert it into the article_id field of the second table (so they match). I am using the mysql_insert_id() command for the first time and I am wondering if I can run a query like this and turn it into a variable or if I need to use a SELECT query from the the mysql_insert_id() field from the first table before inserting it into the second table? Any feedback is appreciated. Thanks, kaiman Here is what I have so far (untested): // insert data into blog database $sql1="INSERT INTO $tbl_name1(author, title, content, date)VALUES('$author', '$title', '$content', NOW()) LIMIT 1"; $result1=mysql_query($sql1) or trigger_error("A mysql error has occurred!"); $article_id = mysql_insert_id (); // if successfully inserted data into database, redirect user if($result1){ header( "Location: http://www.mydomain.com/blog/add/success/" ); } else { header( "Location: http://www.mydomain.com/blog/add/error/" ); exit; } // insert data into blog categories database $sql2="INSERT INTO $tbl_name2(article_id, category)VALUES('$article_id', '$category' LIMIT 1"; $result2=mysql_query($sql2) or trigger_error("A mysql error has occurred!"); // if successfully inserted data into database, redirect user if($result2){ header( "Location: http://www.mydomain.com/blog/add/success/" ); } else { header( "Location: http://www.mydomain.com/blog/add/error/" ); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/224358-is-this-the-best-way-to-use-mysql_insert_id-or-do-i-need-to-select-it/ Share on other sites More sharing options...
Pikachu2000 Posted January 13, 2011 Share Posted January 13, 2011 It should work. Why don't you try it and see what happens? Quote Link to comment https://forums.phpfreaks.com/topic/224358-is-this-the-best-way-to-use-mysql_insert_id-or-do-i-need-to-select-it/#findComment-1159076 Share on other sites More sharing options...
Maq Posted January 13, 2011 Share Posted January 13, 2011 You're using it appropriately and there are ways to check this. An except from the manual: Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). Quote Link to comment https://forums.phpfreaks.com/topic/224358-is-this-the-best-way-to-use-mysql_insert_id-or-do-i-need-to-select-it/#findComment-1159081 Share on other sites More sharing options...
kaiman Posted January 13, 2011 Author Share Posted January 13, 2011 @ Pikachu2000. OK thanks. I was just wondering whether I had the syntax/query structure correct or not. Unfortunately, I have no DB connection at the moment. I will give it a shot when I do and let you know if I have trouble. Thanks again, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/224358-is-this-the-best-way-to-use-mysql_insert_id-or-do-i-need-to-select-it/#findComment-1159084 Share on other sites More sharing options...
kaiman Posted January 13, 2011 Author Share Posted January 13, 2011 One more thing, is there a way to tie these if else statements together? Rather then having two could I do something like: if($result1,$result2){ header( "Location: http://www.mydomain.com/blog/add/success/" ); } else { header( "Location: http://www.mydomain.com/blog/add/error/" ); exit; } Thanks, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/224358-is-this-the-best-way-to-use-mysql_insert_id-or-do-i-need-to-select-it/#findComment-1159085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.