Jump to content

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_id() 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

Code:

<?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/55146-mulitple-update-with-php-mysql/
Share on other sites

Guest
This topic is now 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.