Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Insert and Update Server Behaviors in the Same Form

New Here ,
Apr 29, 2009 Apr 29, 2009

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

TOPICS
Server side applications
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 29, 2009 Apr 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,

...
Translate
LEGEND ,
Apr 29, 2009 Apr 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));
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 29, 2009 Apr 29, 2009

thanks for your help. that did the trick.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 14, 2015 Oct 14, 2015

I've also tried this code.  Thanks for any help!

if (isset($_POST['insert']))

  {

  $OK = false;

  $insertSQL = 'INSERT INTO textpage (home_key, textpage, h_date, h_seq, h_col, p_heading, p_text, h_hide) VALUES (:home_key, :textpage, :h_date, :h_seq, :h_col, :p_heading, :p_text, :h_hide)';

$stmt = $sainttim->prepare($insertSQL);

// bind parameters, execute statement

$stmt->bindParam(':textpage', $_POST['textpage'], PDO::PARAM_STR);

$stmt->bindParam(':h_date', $_POST['h_date'], PDO::PARAM_STR);

$stmt->bindParam(':h_seq', $_POST['h_seq'], PDO::PARAM_STR);

$stmt->bindParam(':h_col', $_POST['h_col'], PDO::PARAM_STR);

$stmt->bindParam(':p_heading', $_POST['head'], PDO::PARAM_STR);

$stmt->bindParam(':p_text', $_POST['p_text'], PDO::PARAM_STR);

$stmt->bindParam(':h_hide', $_POST['h_hide'], PDO::PARAM_STR);

// execute & get # affected rows

$stmt->execute();

$OK = $stmt->rowCount();

// redirect if successful or display error

if ($OK)

{

  header('Location: ', $insertGoTo);

  exit;

} else

{

  $error = $stmt->errorInfo();

  if (isset($error[2])) die($error[2]);

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 14, 2015 Oct 14, 2015
LATEST

FINALLY GOT IT! Using example from your book.  Wrong table name - so dumb!  Now my error is cannot send header, but I've seen that dealt with in the forums and will go look.  Thanks for your book!  I really do live by it....

Gail

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines