[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Update form won't update

<?php
session_start();

require ('header.html');
	
date_default_timezone_set('America/Los_Angeles');

if(isset($_SESSION['valid_user'])){
	echo '<div class="content"><div class="item">';
	require('dbc.php');
	connect_db();
	$query = "Select * FROM SitRepReport WHERE IncidentName = '{$_POST['IncidentName']}'";
		if($r = mysql_query($query)){
			while($row = mysql_fetch_array($r)){
				(variable declarations)
			}
		}
		if(isset($_POST['submit'])){
		
			$problem = FALSE;
	
			if(empty($_POST['A bunch of conditionals'])){
				$problem = TRUE;
				echo'<p><h2>Please go back you missed something!</h2></p>';
			}
		
			if(!$problem){
				$updtQuery = "UPDATE SitRepReport SET Values";
					if(@mysql_query($updtQuery)){
						echo'<div class = "item"><h1>REPORT UPDATED</h1><p>Your report has been updated.</p></div>';
		
					}else{
						echo"<div class = \"item\"><h1>MySQL Error</h1><div class = \"descr\">Error Generated</div><p>Could not submit report because: <b>".mysql_error()."</b>.  The query was $updtQuery.</p></div>";
					}	
			}
		}

	echo'<form>Form is populated by above declared variables using MySQL</form>';
	
	close_db();

}else{
	echo '<h1>YOU ARE NOT LOGGED IN!</h1>';
}
require ('footer.html');
?>

Open in new window

Greetings experts,

I have a reporting form which is stored the date enterd into a MySQL DB.  Once the user has input the data I have given them the option to change said data need be.

The update form is set up to load the previously entered data so the user only need to change the one or two items he/she wishes to change.  Once they click the update button the new data is sent to the DB.  All of which works fine.

The problem I am having is once the update button is clicked the data is being updated in the DB but the form refreshs back to the orinal data and I am afraid this will confuse my users.  They can see the form change if they manually click the refresh button but the too provides a warning about resending data which I fear will also confuse them.

Is there a way to stop this from occuring?  I have tried reload, redirect back to the same page.

I am a beginner in PHP so I don't know all the tricks.

I have included my abriviated code below.
0
taskhill
Asked:
taskhill
  • 3
  • 3
  • 2
  • +1
1 Solution
 
et_meCommented:
Assuming your form is here

echo'<form>Form is populated by above declared variables using MySQL</form>';

Open in new window


You could try put the SELECT query with If else of  isset($_POST['submit']) .
if(isset($_POST['submit'])){
		
			$problem = FALSE;
	
			if(empty($_POST['A bunch of conditionals'])){
				$problem = TRUE;
				echo'<p><h2>Please go back you missed something!</h2></p>';
			}
		
			if(!$problem){
				$updtQuery = "UPDATE SitRepReport SET Values";
					if(@mysql_query($updtQuery)){
						echo'<div class = "item"><h1>REPORT UPDATED</h1><p>Your report has been updated.</p></div>';
		
					}else{
						echo"<div class = \"item\"><h1>MySQL Error</h1><div class = \"descr\">Error Generated</div><p>Could not submit report because: <b>".mysql_error()."</b>.  The query was $updtQuery.</p></div>";
					}	
			}
		}else{
$query = "Select * FROM SitRepReport WHERE IncidentName = '{$_POST['IncidentName']}'";
		if($r = mysql_query($query)){
			while($row = mysql_fetch_array($r)){
				(variable declarations)
			}
		}
}
echo'<form>Form is populated by above declared variables using MySQL</form>';

Open in new window

0
 
taskhillAuthor Commented:
When I make the suggested changes listed above, all of my variable definitions in "(variable declarations)" above all come back as "Undefined" and the script errors.
0
 
et_meCommented:
You would need to retrieve the variable value after

if(!$problem){
				$updtQuery = "UPDATE SitRepReport SET Values";
					if(@mysql_query($updtQuery)){
						echo'<div class = "item"><h1>REPORT UPDATED</h1><p>Your report has been updated.</p></div>';
		
					}else{
						echo"<div class = \"item\"><h1>MySQL Error</h1><div class = \"descr\">Error Generated</div><p>Could not submit report because: <b>".mysql_error()."</b>.  The query was $updtQuery.</p></div>";
					}	
			}
(variable declarations eg: $val=$_POST['val']) 

Open in new window


In this way, it will retrieve the submitted value (either those successfully updated or failed to update or failed the validation.)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
taskhillAuthor Commented:
Okay now that I have made that change the form comes up empty.  Here is what I have
<?php
session_start();
//address error handling
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

//define title of page
define ('TITLE', 'SitRep Update Form');

//Need the header
require ('header.html');
	
date_default_timezone_set('America/Los_Angeles');

if(isset($_SESSION['valid_user'])){
	echo '<div class="content"><div class="item">';
	require('dbc.php');
	connect_db();
	
			if(isset($_POST['submit'])){
		
				$problem = FALSE;
	
				
				if(empty(I have a bunch of conditionals here)){
					$problem = TRUE;
					echo'<p><h2>Please enter your Critical Issues.  If none, enter "None"</h2></p>';
				}
				if(!$problem){
					$updtQuery = "UPDATE SitRepReport SET OrgUpDt='{$_POST['OrgUpDt']}', ReportType='{$_POST['ReportType']}', ReportDate='$Date', ReportTime='$Time', FName='{$_POST['FName']}', LName='{$_POST['LName']}', Position='{$_POST['Position']}', OffPhone='{$_POST['OffPhone']}', AltPhone='{$_POST['AltPhone']}', Fax='{$_POST['Fax']}', email='{$_POST['email']}', ILevel='{$_POST['ILevel']}', ICond='{$_POST['ICond']}', IProg='{$_POST['IProg']}', CurrentSituation='{$_POST['CurrentSituation']}', CurrentPriorities='{$_POST['CurrentPriorities']}', CriticalIssues='{$_POST['CriticalIssues']}', Region='{$_POST['Region']}', OA='{$_POST['OA']}', IncidentName='{$_POST['IncidentName']}', IncDate='{$_POST['IncDate']}', IncTime='{$_POST['IncTime']}', IncAddress='{$_POST['IncAddress']}', IncCity='{$_POST['IncCity']}', IncType='{$_POST['IncType']}', EstPop='{$_POST['EstPop']}', MHPOCName='{$_POST['MHPOCName']}', MHPOCPhone='{$_POST['MHPOCPhone']}', MHPOCEmail='{$_POST['MHPOCEmail']}' WHERE SSRID = $SSRID";
						if(@mysql_query($updtQuery)){
						echo'<div class = "item"><h1>REPORT UPDATED</h1><p>Your report has been updated.</p></div>';
		
					}else{
						echo"<div class = \"item\"><h1>MySQL Error</h1><div class = \"descr\">Error Generated</div><p>Could not submit report because: <b>".mysql_error()."</b>.  The query was $updtQuery.</p></div>";
					}
				}
			}else{		
				$query = "Select * FROM SitRepReport WHERE IncidentName = '{$_POST['IncidentName']}'";
					if($r = mysql_query($query)){
						while($row = mysql_fetch_array($r)){}
					}
				$SSRID = $row[0];
				$OrgUpDt = $row[1];
				$ReportType = $row[2];
				$ReportDate = $row[3];
				$ReportTime = $row[4];
				$UserID = $row[5];
				$FName = $row[6];
				$LName = $row[7];
				$Position = $row[8];
				$OffPhone = $row[9];
				$AltPhone = $row[10];
				$Fax = $row[11];
				$email = $row[12];
				$ILevel = $row[13];
				$ICond = $row[14];
				$IProg = $row[15];
				$CurrentSituation = $row[16];
				$CurrentPriorities = $row[17];
				$CriticalIssues = $row[18];
				$Region = $row[19];
				$OA = $row[20];
				$IncidentName = $_POST['IncidentName'];
				$IncDate = $row[22];
				$IncTime = $row[23];
				$IncAddress = $row[24];
				$IncCity = $row[25];
				$IncType = $row[26];
				$EstPop = $row[27];
				$MHPOCName = $row[28];
				$MHPOCPhone = $row[29];
				$MHPOCEmail = $row[30];
				$Date = date('Y-m-d');
				$Time = date('H:i:s');
			}	
echo'<form>My form is here</form>';
close_db();
}else{
	echo '<div class="content">
		<div class="item">
		<h1>YOU ARE NOT LOGGED IN!</h1>
		<div class="descr">August 9, 2012</div>
		<p>Either your session has expired or you did not log in.</p>
		<p>Please log in.</p></div></div>';
}
require ('footer.html');
?>

Open in new window

0
 
et_meCommented:
<?php
session_start();
//address error handling
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

//define title of page
define ('TITLE', 'SitRep Update Form');

//Need the header
require ('header.html');
	
date_default_timezone_set('America/Los_Angeles');

if(isset($_SESSION['valid_user'])){
	echo '<div class="content"><div class="item">';
	require('dbc.php');
	connect_db();
	
			if(isset($_POST['submit'])){
		
				$problem = FALSE;
	
				
				if(empty(I have a bunch of conditionals here)){
					$problem = TRUE;
					echo'<p><h2>Please enter your Critical Issues.  If none, enter "None"</h2></p>';
				}
				if(!$problem){
					$updtQuery = "UPDATE SitRepReport SET OrgUpDt='{$_POST['OrgUpDt']}', ReportType='{$_POST['ReportType']}', ReportDate='$Date', ReportTime='$Time', FName='{$_POST['FName']}', LName='{$_POST['LName']}', Position='{$_POST['Position']}', OffPhone='{$_POST['OffPhone']}', AltPhone='{$_POST['AltPhone']}', Fax='{$_POST['Fax']}', email='{$_POST['email']}', ILevel='{$_POST['ILevel']}', ICond='{$_POST['ICond']}', IProg='{$_POST['IProg']}', CurrentSituation='{$_POST['CurrentSituation']}', CurrentPriorities='{$_POST['CurrentPriorities']}', CriticalIssues='{$_POST['CriticalIssues']}', Region='{$_POST['Region']}', OA='{$_POST['OA']}', IncidentName='{$_POST['IncidentName']}', IncDate='{$_POST['IncDate']}', IncTime='{$_POST['IncTime']}', IncAddress='{$_POST['IncAddress']}', IncCity='{$_POST['IncCity']}', IncType='{$_POST['IncType']}', EstPop='{$_POST['EstPop']}', MHPOCName='{$_POST['MHPOCName']}', MHPOCPhone='{$_POST['MHPOCPhone']}', MHPOCEmail='{$_POST['MHPOCEmail']}' WHERE SSRID = $SSRID";
						if(@mysql_query($updtQuery)){
						echo'<div class = "item"><h1>REPORT UPDATED</h1><p>Your report has been updated.</p></div>';
		
					}else{
						echo"<div class = \"item\"><h1>MySQL Error</h1><div class = \"descr\">Error Generated</div><p>Could not submit report because: <b>".mysql_error()."</b>.  The query was $updtQuery.</p></div>";
					}
				}
                                // to retrieve the submitted value
                                $SSRID = $POST['SSRID '];
				$OrgUpDt = $POST['OrgUpDt'];
				$ReportType = $POST['ReportType '];
				.....
                                .....
			}else{		
				$query = "Select * FROM SitRepReport WHERE IncidentName = '{$_POST['IncidentName']}'";
					if($r = mysql_query($query)){
						while($row = mysql_fetch_array($r)){}
					}
				$SSRID = $row[0];
				$OrgUpDt = $row[1];
				$ReportType = $row[2];
				$ReportDate = $row[3];
				$ReportTime = $row[4];
				$UserID = $row[5];
				$FName = $row[6];
				$LName = $row[7];
				$Position = $row[8];
				$OffPhone = $row[9];
				$AltPhone = $row[10];
				$Fax = $row[11];
				$email = $row[12];
				$ILevel = $row[13];
				$ICond = $row[14];
				$IProg = $row[15];
				$CurrentSituation = $row[16];
				$CurrentPriorities = $row[17];
				$CriticalIssues = $row[18];
				$Region = $row[19];
				$OA = $row[20];
				$IncidentName = $_POST['IncidentName'];
				$IncDate = $row[22];
				$IncTime = $row[23];
				$IncAddress = $row[24];
				$IncCity = $row[25];
				$IncType = $row[26];
				$EstPop = $row[27];
				$MHPOCName = $row[28];
				$MHPOCPhone = $row[29];
				$MHPOCEmail = $row[30];
				$Date = date('Y-m-d');
				$Time = date('H:i:s');
			}	
echo'<form>My form is here</form>';
close_db();
}else{
	echo '<div class="content">
		<div class="item">
		<h1>YOU ARE NOT LOGGED IN!</h1>
		<div class="descr">August 9, 2012</div>
		<p>Either your session has expired or you did not log in.</p>
		<p>Please log in.</p></div></div>';
}
require ('footer.html');
?>

Open in new window

0
 
taskhillAuthor Commented:
That did not work either but it may be an error on my part.  I will look closer in the morning.
0
 
CWS (haripriya)Commented:
Don't give the form code in else part.
<?php
session_start();
//address error handling
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

//define title of page
define ('TITLE', 'SitRep Update Form');

//Need the header
require ('header.html');
	
date_default_timezone_set('America/Los_Angeles');

if(isset($_SESSION['valid_user'])){
	echo '<div class="content"><div class="item">';
	require('dbc.php');
	connect_db();
	
			if(isset($_POST['submit'])){
		
				$problem = FALSE;
	
				
				if(empty(I have a bunch of conditionals here)){
					$problem = TRUE;
					echo'<p><h2>Please enter your Critical Issues.  If none, enter "None"</h2></p>';
				}
				if(!$problem){
					$updtQuery = "UPDATE SitRepReport SET OrgUpDt='{$_POST['OrgUpDt']}', ReportType='{$_POST['ReportType']}', ReportDate='$Date', ReportTime='$Time', FName='{$_POST['FName']}', LName='{$_POST['LName']}', Position='{$_POST['Position']}', OffPhone='{$_POST['OffPhone']}', AltPhone='{$_POST['AltPhone']}', Fax='{$_POST['Fax']}', email='{$_POST['email']}', ILevel='{$_POST['ILevel']}', ICond='{$_POST['ICond']}', IProg='{$_POST['IProg']}', CurrentSituation='{$_POST['CurrentSituation']}', CurrentPriorities='{$_POST['CurrentPriorities']}', CriticalIssues='{$_POST['CriticalIssues']}', Region='{$_POST['Region']}', OA='{$_POST['OA']}', IncidentName='{$_POST['IncidentName']}', IncDate='{$_POST['IncDate']}', IncTime='{$_POST['IncTime']}', IncAddress='{$_POST['IncAddress']}', IncCity='{$_POST['IncCity']}', IncType='{$_POST['IncType']}', EstPop='{$_POST['EstPop']}', MHPOCName='{$_POST['MHPOCName']}', MHPOCPhone='{$_POST['MHPOCPhone']}', MHPOCEmail='{$_POST['MHPOCEmail']}' WHERE SSRID = $SSRID";
						if(@mysql_query($updtQuery)){
						echo'<div class = "item"><h1>REPORT UPDATED</h1><p>Your report has been updated.</p></div>';
		
					}else{
						echo"<div class = \"item\"><h1>MySQL Error</h1><div class = \"descr\">Error Generated</div><p>Could not submit report because: <b>".mysql_error()."</b>.  The query was $updtQuery.</p></div>";
					}
				}
			}		
				$query = "Select * FROM SitRepReport WHERE IncidentName = '{$_POST['IncidentName']}'";
					if($r = mysql_query($query)){
						while($row = mysql_fetch_array($r)){
					
				$SSRID = $row[0];
				$OrgUpDt = $row[1];
				$ReportType = $row[2];
				$ReportDate = $row[3];
				$ReportTime = $row[4];
				$UserID = $row[5];
				$FName = $row[6];
				$LName = $row[7];
				$Position = $row[8];
				$OffPhone = $row[9];
				$AltPhone = $row[10];
				$Fax = $row[11];
				$email = $row[12];
				$ILevel = $row[13];
				$ICond = $row[14];
				$IProg = $row[15];
				$CurrentSituation = $row[16];
				$CurrentPriorities = $row[17];
				$CriticalIssues = $row[18];
				$Region = $row[19];
				$OA = $row[20];
				$IncidentName = $_POST['IncidentName'];
				$IncDate = $row[22];
				$IncTime = $row[23];
				$IncAddress = $row[24];
				$IncCity = $row[25];
				$IncType = $row[26];
				$EstPop = $row[27];
				$MHPOCName = $row[28];
				$MHPOCPhone = $row[29];
				$MHPOCEmail = $row[30];
				$Date = date('Y-m-d');
				$Time = date('H:i:s');
			}
}
echo'<form>My form is here</form>';
close_db();
}else{
	echo '<div class="content">
		<div class="item">
		<h1>YOU ARE NOT LOGGED IN!</h1>
		<div class="descr">August 9, 2012</div>
		<p>Either your session has expired or you did not log in.</p>
		<p>Please log in.</p></div></div>';
}
require ('footer.html');
?>

Open in new window

0
 
Ray PaseurCommented:
The general design principle you would follow for something like this:

The form script
1. Look for a GET-method request containing a key.
2. If no GET key is present, this is an original request.  Put up a blank form.
3. If a GET key is present, this is a repeated request.  Use the key to retrieve the associated data from the data base and prepopulate the form's value= attributes.

The action script
1. Look for a GET-method request containing a key.
2. If no GET key is present, this is an original request.  INSERT the data.
3. IF a GET key is present, this is a repeated request.  Use the key in the WHERE clause to UPDATE the data.
4. Redirect to the form script, adding the key to the URL.

This coding is greatly facilitated by using HEREDOC notation for the HTML form.  When the client has finished additions and updates, she simply navigates away from the web page.  You might have an "all done" link near the submit button to facilitate this.
0
 
Ray PaseurCommented:
Here is a code snippet that illustrates some of the moving parts.  It remembers the client input and highlights errors or omissions.  Obviously the data "validation" is pretty simplistic - all we check for is the presence of something in the input controls.

The data base INSERT or UPDATE would be done at line 45.  The prepopulation of the value= attributes would be done near line 10, where we construct the $required variable.

<?php // RAY_form example.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL; // JUST FOR READABILITY


// QUESTION - HOW DO I FIND WHAT WAS POSTED AND CAPTURE IT FOR FURTHER PROCESSING
// QUESTION - HOW DO I TELL THE CLIENT TO ENTER THE OTHER FIELDS THAT WERE OMITTED


// THE FIELDS WE EXPECT FROM THE FORM WILL BE KEPT HERE
$required
= array
( "name"  => "Your Name"
, "email" => "Email Address"
, "phone" => "Telephone"
)
;

// ERROR MESSAGES, RECEIVED DATA, IF ANY, ARE HERE
$received      = array();
$error_fields  = array();
$error_message = '';

// IF THE FORM HAS BEEN SUBMITTED THE POST ARRAY IS NOT EMPTY
if (!empty($_POST))
{
    // WIPE OUT ANY POST VALUES THAT STILL CONTAIN THE DEFAULT FIELD VALUES
    foreach ($_POST as $key => $val)
    {
        if (trim($required[$key]) == trim($val)) unset($_POST[$key]);
    }

    // CHECK TO SEE IF EACH REQUIRED FIELD IS PRESENT
    foreach($required as $key => $val)
    {
        // IF THIS FIELD IS EMPTY - SET THE ERROR MESSAGE AND SAVE THE KEY (OTHER EDITS MAY APPLY)
        if (empty($_POST[$key]))
        {
            $error_message  .= PHP_EOL . "PLEASE ENTER $val";
            $error_fields[] = $key;
        }
        // IF THIS FIELD IS PRESENT AND VALID CAPTURE THE CONTENTS
        else
        {
            $received[$key] = $_POST[$key];
        }
    }

    // DISPLAY THE ERROR MESSAGE, IF ANY
    echo $error_message;

    // ACTIVATE THIS TO SHOW WHAT FIELDS WE CAPTURED
    // echo PHP_EOL . 'WE RECEIVED:' . PHP_EOL;
    // print_r($received);

    // IF NO ERRORS, PROCESSING COMPLETE (MAYBE UPDATE DATA BASE NOW?)
    if (empty($error_message))
    {
        die('THANK YOU');
    }
} // END OF POST DATA PROCESSING

// END OF PROCESSING THE SUBMITTED INPUT - PUT UP THE FORM TO CAPTURE INPUT

echo '<form method="post">';
foreach ($required as $key => $value)
{
    echo "<br/>";

    // MAKE ERROR FIELDS SHOW IN RED
    if (in_array($key, $error_fields))
    {
        echo '<span style="color:red;">';
    }
    else
    {
        echo '<span>';
    }

    // PRESERVE THE VALUES THAT HAVE BEEN ENTERED
    if (isset($received[$key]))
    {
        $display_value = $received[$key];
    }
    else
    {
        $display_value = $value;
    }

    // PRODUCE THE INPUT CLUE
    echo "$value: </span>";

    // PRODUCE THE INPUT CONTROL
    echo '<input name="' . $key . '" value="' . $display_value . '" />';
}
echo '<input type="submit" />';
echo '</form>';

Open in new window

HTH, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now