Jump to content

Multiple Table Insert


chillininvt

Recommended Posts

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;
}
?>

Link to comment
Share on other sites

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 {

 

 

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.