Jump to content

[SOLVED] adding primary key and foreign key to two table using one form


mattennant

Recommended Posts

Hi There

 

I want to insert data from one form into two different tables with one form submit, i want the primary key from 'question' table (main_id) to be the foreign key in  the  'question_intro'

 

Is there a way to do this? The only way can think is to get the latest primary key from the 'question' table in a hidden field add 1 to it and insert that number as the foreign key, but that all feels a bit clumsy.

 

here's my code

// this tables auto incrementing primary key is called main_id
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO question (task_ref, step_ref, step_type) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['task_ref'], "int"),
                       GetSQLValueString($_POST['step_ref'], "int"),
				   GetSQLValueString($_POST['step_type'], "int"));

  mysql_select_db($database_mattyboy, $mattyboy);
  $Result1 = mysql_query($insertSQL, $mattyboy) or die(mysql_error());
  
  // iwant to pass the primary key from the above table to this table as the foreign key
   $insertSQL2 = sprintf("INSERT INTO question_intro (main_id, task_ref, question) VALUES (%s, %s, %s)",
   					   GetSQLValueString($_POST['main_id'], "int"),// maybe get this from a  hidden field
                       GetSQLValueString($_POST['task_ref'], "int"),
                       GetSQLValueString($_POST['question'], "text"));

  mysql_select_db($database_mattyboy, $mattyboy);
  $Result2 = mysql_query($insertSQL2, $mattyboy) or die(mysql_error());

 

Thanks

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.