Copy link to clipboard
Copied
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));
}
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,
Copy link to clipboard
Copied
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));
}
Copy link to clipboard
Copied
thanks for your help. that did the trick.
Copy link to clipboard
Copied
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]);
}
}
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now