vynsane Posted March 8, 2007 Share Posted March 8, 2007 Hi, as the subject says, I'm trying to do two queries at once utilizing data from a form. I'm creating a new row in one table, and I need to reflect the ID number and other supplementary data to an existing record in another table. Is there a way to string two queries together so they execute at the same time? Here's what I've been trying so far: elseif (isset($_POST['submit'])): $newthing = $_POST['newthing']; $othernewthing = $_POST['othernewthing']; $updatedthing = $_POST['updatedthing']; $otherupdatedthing = $_POST['otherupdatedthing']; $sql = "INSERT INTO table SET newthing=$newthing, othernewthing=$othernewthing"; "UPDATE othertable SET updatedthing=$updatedthing, otherupdatedthing=$otherupdatedthing"; if (mysql_query($sql)) { echo "<div class='fieldbox'>\n"; echo "<h2><span>New things added, other things updated!</span></h2>\n"; } The first part of the query works, but the update doesn't run. If I connect them together by getting rid of the quotes and the semicolon between them, I get errors on submit and neither part of the query works. What can I do here? I tried other variations I could think of, but this is the only one that gets even some results. Link to comment https://forums.phpfreaks.com/topic/41837-solved-trying-to-insert-to-one-table-update-to-another/ Share on other sites More sharing options...
hitman6003 Posted March 8, 2007 Share Posted March 8, 2007 YOu have to do two seperate queries.... mysql_query("INSERT INTO table (newthing, othernewthing) VALUES ('$newthing', '$othernewthing')") or die(mysql_error()); mysql_query("UPDATE othertable SET updatedthing=$updatedthing, otherupdatedthing=$otherupdatedthing WHERE id = " . mysql_insert_id()) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/41837-solved-trying-to-insert-to-one-table-update-to-another/#findComment-202890 Share on other sites More sharing options...
vynsane Posted March 9, 2007 Author Share Posted March 9, 2007 I simplified the page I was making, and got rid of the update - but thanks for you help anyway. Link to comment https://forums.phpfreaks.com/topic/41837-solved-trying-to-insert-to-one-table-update-to-another/#findComment-203116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.