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

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.