treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Adding data from an insert form into two tables

  • I'm working in Dreamweaver and I need to create an insert form that will add the data from the form into two tables in the database. I've found tons of instructions for creating a form to insert data into one table, but I can not find a tutorial that will take me through the steps of how to insert data into two tables in one transaction with one insert form. Does anyone have a great tutorial out there on this?
  • You cannot do it, you will need to run 2 query's
  • And as I'm guessing there will need to be a common ID between the two tables, you will probably need to use the mysql_insert_id function at some point
  • Thanks for the heads up. Does anyone have any good tutorials on this? I need a tut that will take me through the steps. Thanks in advance for your help on this.
  • Well basically you start by inserting data into one table. Then, to get the unique ID of the record you just added (assuming you have an auto-increment primary key field), you do something like
    $lastID = mysql_insert_id();

    Then you can use that ID as a foreign key when inserting data into the second table
  • You could always create a "unique identifier" for each entry on your own. I don't know what exactly you are storing into the db, but I'll give a generic example.


    //generate unique serial off of the current date, easy enough
    $unique = date(U);
    $name = $_POST['name'];
    $email = $_POST['email'];
    //and so on,

    //run your two queries, you can link them by puting that $unique var into both tables, then when you query for them you can use \"WHERE unique = '$unique'\" or something of the sort.


    if that makes any sense at all.