Jump to content

Multiple table insert with php mysql


chillininvt

Recommended Posts

Ok the problem is this i need to run 3 queries. The first query will insert a record into the database. It will have an auto increment id vaule when it is inserted. This vaule is what we are going to call the $departmentid I need to grab this value then store it into a variable so I can update 2 other tables with this vaule. Here is the code. The section of code that I am refering to is contained within the coditional if( $department_id == "" ) { What I need to do essentially is insert the record retrieve the insert id vaule the store data in 2 more tabes each of which will contain the insert id number. This script appears that it would do the job so long as $departmentid == "" and something gets assigned to $departmentid . Could someone tell me wy this script will only exectue the first line of sql code. Even if I take out the if conditional to test for the value of $departmentid it will still only execute one line of sql. I need to be able to exectue a sequece of sql statements when this code block is executed any ideas. I will send a tip via paypal for any advice that works.

 

<?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
https://forums.phpfreaks.com/topic/54950-multiple-table-insert-with-php-mysql/
Share on other sites

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.