chillininvt Posted June 11, 2007 Share Posted June 11, 2007 Ok I have a table that when you add a new department it will add the name to the table and assign the row and id based on an auto_increment column. From there you need to get the value of the last insert id. SO what I did was use the msql_insert_is() function. and assigned it to a variable. I then used that variable in my next query because this vaule is a forigen key in another table. Here is my code it should be exectued if you where to post this page with the value of $department_id = "" then the section of script in question will execture. Essentially need it to grab this id number and then place it into 2 more insert statements that should execute immediately after the first insert statement. Then only insert id that I would need is the one produced in the first insert statement what an auto increment value is assigned to the same table as the department_name <?php // validates on the pageID of the Calendar page require( "../utils/auth.php" ); $auth = mysql_fetch_array(mysql_query("SELECT pageID FROM PageLocations WHERE location = 'contact.php'")); $pID = $auth['pageID']; require( "../access/access_via_passed_pageID.php" ); // to flag that a user has visited the page toolset (but not necessarily changed anything) include("../inc/after_user_verify.php"); // CITY DIRECTORY PAGES UPDATE DIRECTLY TO THE LIVE DATABASE and skip the three-tier system require( "../inc/conn_live.php" ); require_once( "../utils/string_utils.php" ); if( isset($_POST['btnSubmit']) ) { $department_id = $_GET['department_id']; if( $department_id == "" ) { // ADD NEW mysql_query("INSERT INTO CityHallDepartment (department_name) VALUES ('" . $_POST['department_name'] . "');"); echo "This is the department id".$departmentid.""; $departmentname = $_POST['department_name']; if($departmentname != NULL){ $departmentid = mysql_insert_id(); mysql_query("INSERT INTO PageGeneric (pid, bannerid, title, content, metadesc, layoutID) VALUES ('0', '$departmentname', 'Comming Soon', '$departmentname', '3');"); echo"The script executed into while loop"; $page = "generalpage.php?page=".$departmentid.""; mysql_query("INSERT INTO PageLocations (parentID, location, title, display, displayNavCMS, departmentID) VALUES ('0', '$page', '$departmentname', '1', '1', '0', $departmentid);"); }else{ echo"The script when to the else sections"; } } else { // UPDATE EXISTING mysql_query("UPDATE CityHallDepartment SET department_name = '" . $_POST['department_name'] . "' WHERE id = '" . $department_id . "'"); if( isset($_POST['main_contact']) ) { // clear the current main contact flag ... mysql_query("UPDATE CityHallDirectory SET department_head = 0 WHERE department_id = '" . $department_id . "'"); // ... then set the new main contact flag mysql_query("UPDATE CityHallDirectory SET department_head = 1 WHERE department_id = '" . $department_id . "' AND id = '" . $_POST['main_contact'] . "'"); } } header("Location: contact.php"); exit; } else { if( isset($_POST['department_id']) ) { // EDIT $department_id = $_POST['department_id']; } else { // ADD $department_id = ""; } $r_dept = mysql_query("SELECT * FROM CityHallDepartment WHERE id = '" . $department_id . "'"); $m_dept = mysql_fetch_array( $r_dept ); $department_name = $m_dept['department_name']; } if ( isset($_POST['btnDelete']) ) { $department_id = $_GET['department_id']; mysql_query("DELETE FROM CityHallDepartment WHERE id = '" . $department_id . "'"); header("Location: contact.php"); exit; } if ( isset($_POST['btnCancel']) ) { header("Location: contact.php"); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55041-multiple-table-insert/ Share on other sites More sharing options...
btherl Posted June 11, 2007 Share Posted June 11, 2007 What goes wrong when you run your script? Quote Link to comment https://forums.phpfreaks.com/topic/55041-multiple-table-insert/#findComment-272093 Share on other sites More sharing options...
redarrow Posted June 11, 2007 Share Posted June 11, 2007 take all the $_POST[''] out of the querys and set them sepratly example $a=addslashes($_POST['a']); afther doing all that will work ok. Quote Link to comment https://forums.phpfreaks.com/topic/55041-multiple-table-insert/#findComment-272097 Share on other sites More sharing options...
chillininvt Posted June 11, 2007 Author Share Posted June 11, 2007 When it gets to this section of code. It will only execute the first insert statement. The other two queries are not executing. I even tried pulling it out of the if($departmentname != NULL){ conditional statement it they still will not exectue. What I would like to do is run a query so that when a column is created in the first table I can also create 2 more columns in two other tables. I cannot seem to get this for the life of me and it seems so simple that its embarrassing having to ask this question up here. All I need to do essentially is execute one query after the other. Mabe you have a code snippet of a php script that exectues multiple queries one after the other and try to make it work with this script. if( $department_id == "" ) { // ADD NEW mysql_query("INSERT INTO CityHallDepartment (department_name) VALUES ('" . $_POST['department_name'] . "');"); echo "This is the department id".$departmentid.""; $departmentname = $_POST['department_name']; if($departmentname != NULL){ $departmentid = mysql_insert_id(); mysql_query("INSERT INTO PageGeneric (pid, bannerid, title, content, metadesc, layoutID) VALUES ('0', '$departmentname', 'Comming Soon', '$departmentname', '3');"); echo"The script executed into while loop"; $page = "generalpage.php?page=".$departmentid.""; mysql_query("INSERT INTO PageLocations (parentID, location, title, display, displayNavCMS, departmentID) VALUES ('0', '$page', '$departmentname', '1', '1', '0', $departmentid);"); }else{ echo"The script when to the else sections"; } } else { Quote Link to comment https://forums.phpfreaks.com/topic/55041-multiple-table-insert/#findComment-272168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.