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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.