mattennant Posted April 2, 2008 Share Posted April 2, 2008 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 https://forums.phpfreaks.com/topic/99160-solved-adding-primary-key-and-foreign-key-to-two-table-using-one-form/ Share on other sites More sharing options...
mattennant Posted April 2, 2008 Author Share Posted April 2, 2008 found it if anyone is interested mysql_insert_id is the answer http://uk.php.net/mysql_insert_id Link to comment https://forums.phpfreaks.com/topic/99160-solved-adding-primary-key-and-foreign-key-to-two-table-using-one-form/#findComment-507593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.