Skip to main content
Known Participant
April 29, 2009
Answered

Insert and Update Server Behaviors in the Same Form

  • April 29, 2009
  • 4 replies
  • 1538 views

I have a php form that is inserting into one table and then updating another table. I have the form inserting the record first and then I want it to update the other table. I am starting to learn and understand php, but I don't know how to have the update function perform after it inserts the records. I suppose it is something along the lines if you insert this record, then update this record and then go to the redirect page. I do know that it is going to the redirect page after it inserts the record, but I don't know how to tell it not to go to the $insertGoTo. I want it to run the next update function and then go to the $insertGoTo and not the $updateGoTo. Thanks in advance for any help.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO employee (CompId, empfirstname, empmiddleint, emplastname, EmpType, EmpStatus) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['CompId'], "int"),
                       GetSQLValueString($_POST['empfirstname'], "text"),
                       GetSQLValueString($_POST['empmiddleint'], "text"),
                       GetSQLValueString($_POST['emplastname'], "text"),
                       GetSQLValueString($_POST['EmpType'], "text"),
                       GetSQLValueString($_POST['EmpStatus'], "text"));

  mysql_select_db($database_dotweb, $dotweb);
  $Result1 = mysql_query($insertSQL, $dotweb) or die(mysql_error());

  $insertGoTo = "Roster.php?CompId=" . $_POST['CompId'] . "";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE company SET CompanyType=%s WHERE CompId=%s",
                       GetSQLValueString($_POST['CompanyType'], "text"),
                       GetSQLValueString($_POST['CompId'], "int"));

  mysql_select_db($database_dotweb, $dotweb);
  $Result1 = mysql_query($updateSQL, $dotweb) or die(mysql_error());

  $updateGoTo = "purchases.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
 
}

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer David_Powers

PHP code is executed from top to bottom, and is controlled by conditional statements ("if" blocks). To achieve what you want, you need to combine the conditional statements that control the insert and update server behaviors, and to move the redirect to the end of the code. Amend it like this:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO employee (CompId, empfirstname, empmiddleint, emplastname, EmpType, EmpStatus) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['CompId'], "int"),
                       GetSQLValueString($_POST['empfirstname'], "text"),
                       GetSQLValueString($_POST['empmiddleint'], "text"),
                       GetSQLValueString($_POST['emplastname'], "text"),
                       GetSQLValueString($_POST['EmpType'], "text"),
                       GetSQLValueString($_POST['EmpStatus'], "text"));
  mysql_select_db($database_dotweb, $dotweb);
  $Result1 = mysql_query($insertSQL, $dotweb) or die(mysql_error());


  $updateSQL = sprintf("UPDATE company SET CompanyType=%s WHERE CompId=%s",
                       GetSQLValueString($_POST['CompanyType'], "text"),
                       GetSQLValueString($_POST['CompId'], "int"));
  mysql_select_db($database_dotweb, $dotweb);
  $Result1 = mysql_query($updateSQL, $dotweb) or die(mysql_error()); 

$insertGoTo = "Roster.php?CompId=" . $_POST['CompId'] . "";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));
}

4 replies

David_Powers
David_PowersCorrect answer
Inspiring
April 29, 2009

PHP code is executed from top to bottom, and is controlled by conditional statements ("if" blocks). To achieve what you want, you need to combine the conditional statements that control the insert and update server behaviors, and to move the redirect to the end of the code. Amend it like this:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO employee (CompId, empfirstname, empmiddleint, emplastname, EmpType, EmpStatus) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['CompId'], "int"),
                       GetSQLValueString($_POST['empfirstname'], "text"),
                       GetSQLValueString($_POST['empmiddleint'], "text"),
                       GetSQLValueString($_POST['emplastname'], "text"),
                       GetSQLValueString($_POST['EmpType'], "text"),
                       GetSQLValueString($_POST['EmpStatus'], "text"));
  mysql_select_db($database_dotweb, $dotweb);
  $Result1 = mysql_query($insertSQL, $dotweb) or die(mysql_error());


  $updateSQL = sprintf("UPDATE company SET CompanyType=%s WHERE CompId=%s",
                       GetSQLValueString($_POST['CompanyType'], "text"),
                       GetSQLValueString($_POST['CompId'], "int"));
  mysql_select_db($database_dotweb, $dotweb);
  $Result1 = mysql_query($updateSQL, $dotweb) or die(mysql_error()); 

$insertGoTo = "Roster.php?CompId=" . $_POST['CompId'] . "";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));
}

kmlohrAuthor
Known Participant
April 30, 2009

thanks for your help. that did the trick.