Jump to content

[SOLVED] Trying to Insert to one table, update to another


vynsane

Recommended Posts

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.

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());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.