Skip navigation
Greg Heffner
Currently Being Moderated

Posting data to multiple linked tables

May 19, 2012 7:49 PM

I am trying to post data from a single form into 3 seperate tables linked by primary and forign keys (all one to many relationships).

I am just a beginner, but it sounded simple to me anyway. I spent several days trying to get it to work, Nada!

 

The form is broken down into 3 insert statements

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "Driver Registration")) {
  $insertSQL = sprintf("INSERT INTO race_database (association, district, event_name, event_date, event_location, open_registration, close_registration, first_boat_cost, add_boat_cost, additional_info) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['association'], "text"),
                       GetSQLValueString($_POST['district'], "text"),
                       GetSQLValueString($_POST['event'], "text"),
                       GetSQLValueString($_POST['event_date'], "date"),
                       GetSQLValueString($_POST['event_location'], "text"),
                       GetSQLValueString($_POST['open_registration'], "date"),
                       GetSQLValueString($_POST['closed_registration'], "date"),
                       GetSQLValueString($_POST['first_boat_cost'], "int"),
                       GetSQLValueString($_POST['add_boat_cost'], "int"),
                       GetSQLValueString($_POST['additional_info'], "text"));

  mysql_select_db($database_RTP_1080, $RTP_1080);
  $Result1 = mysql_query($insertSQL, $RTP_1080) or die(mysql_error());
}

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "Driver Registration")) {
  $insertSQL = sprintf("INSERT INTO driver_database (first_name, last_name, address, city_town, zip_code, `state`, phone_number, mobile_number, e_mail, association_number) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, now())",
                       GetSQLValueString($_POST['firstname'], "text"),
                       GetSQLValueString($_POST['lastname'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['citytown'], "text"),
                       GetSQLValueString($_POST['zipcode'], "int"),
                       GetSQLValueString($_POST['state'], "text"),
                       GetSQLValueString($_POST['phonenumber'], "int"),
                       GetSQLValueString($_POST['mobile'], "int"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['associationnumber'], "text"));

  mysql_select_db($database_RTP_1080, $RTP_1080);
  $Result1 = mysql_query($insertSQL, $RTP_1080) or die(mysql_error());
}

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "Driver Registration")) {
  $insertSQL = sprintf("INSERT INTO boat_database (boat_class, main_frequency, alt_frequency, high_points, cost) VALUES (%s, %s, %s, %s, %s, now())",
                       GetSQLValueString($_POST['boatclass'], "text"),
                       GetSQLValueString($_POST['mainfreq'], "text"),
                       GetSQLValueString($_POST['altfreq'], "text"),
                       GetSQLValueString(isset($_POST['highpoints']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['cost'], "int"));

  mysql_select_db($database_RTP_1080, $RTP_1080);
  $Result1 = mysql_query($insertSQL, $RTP_1080) or die(mysql_error());
}

 

 

The relationships are shown here

relations.jpg

My best results, are that I get data in the first table but none in both joined tables.

 

I am asuming it has to do with the way im inserting the data, but after some reseach, i have seen others have had similar problems, all which I could not understand.

 

Im sure this is a common problem just need some direction.

Thanks

Greg

 
Replies
  • Currently Being Moderated
    May 20, 2012 5:08 PM   in reply to Greg Heffner

    You also need to include the linked ID into your table. To find the ID of the last inserted record see here http://php.net/manual/en/function.mysql-insert-id.php

     

    Gramps

     
    |
    Mark as:
  • Currently Being Moderated
    May 23, 2012 7:20 PM   in reply to Greg Heffner

    The following will illustrate how to insert the data into the database using UserID as the relationship between the two tables

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

      $insertSQL = sprintf("INSERT INTO users (UserID, UserFirstName, UserLastName) VALUES (%s, %s, %s)",

                           GetSQLValueString($_POST['UserID'], "int"),

                           GetSQLValueString($_POST['UserFirstName'], "text"),

                           GetSQLValueString($_POST['UserLastName'], "text"));

     

      mysql_select_db($database_myConnection, $myConnection);

      $Result1 = mysql_query($insertSQL, $myConnection) or die(mysql_error());

     

      $insertSQL = sprintf("INSERT INTO user_address (UserAddressID, UserID, Street, City) VALUES (%s, %s, %s, %s)",

                           GetSQLValueString($_POST['UserAddressID'], "int"),

                           GetSQLValueString(LAST_INSERT_ID(), "int"), // the ID value from the 1st table

                           GetSQLValueString($_POST['Street'], "text"),

                           GetSQLValueString($_POST['City'], "text"));

     

      mysql_select_db($database_myConnection, $myConnection);

      $Result1 = mysql_query($insertSQL, $myConnection) or die(mysql_error());

    }

    Gramps

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points