Checking if Form Data Changed

I have a pretty simple User Details form, and I need to be able to display an outcome page for each of these scenarios…

  • Your User Details were updated
  • Your User Details have not changed
  • Your User Details could not be updated due to a System Error

Here is he code I have…


	// ************************
	// Update Member Record.	*
	// ************************

	// Build query.
	$q1 = "UPDATE member
			SET first_name=?,
				gender=?,
				birth_year=?,
				location=?,
				occupation=?,
				interests=?,
				about_me=?,
				updated_on=NOW()
			WHERE id=?
			LIMIT 1";

	// Prepare statement.
	$stmt1 = mysqli_prepare($dbc, $q1);

	// Bind variables to query.
	mysqli_stmt_bind_param($stmt1, 'ssissssi', $firstName, $gender, $birthYear, $location,
									$occupation, $interests, $aboutMe, $sessMemberID);

	// Execute query.
	mysqli_stmt_execute($stmt1);

	// Verify Update.
	if (mysqli_stmt_affected_rows($stmt1)==1){
		// Update Succeeded.
		$detailsChanged = TRUE;

		// Update Session Variable.
		$_SESSION['sessFirstName'] = $firstName;

	}else{
		// Update Failed.
		$_SESSION['resultsCode'] = 'DETAILS_NEW_DETAILS_FAILED_2132';

		// Set Error Source.
		$_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

		// Redirect to Display Outcome.
		header("Location: " . BASE_URL . "/account/results.php");

		// End script.
		exit();

	}//End of UPDATE MEMBER RECORD

	// Display Form Outcome
	if (empty($_SESSION['resultsCode'])){
		if ($detailsChanged == TRUE){
			// Changes Succeeded.
			$_SESSION['resultsCode'] = 'DETAILS_NEW_DETAILS_SET_2131';

		}elseif ($detailChanged == FALSE){
			// No Changes.
			$_SESSION['resultsCode'] = 'DETAILS_NO_CHANGES_2272';
		}

		// Set Error Source.
		$_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

		// Redirect to Outcome Page.
		header("Location: " . BASE_URL . "/account/results.php");

		// End script.
		exit();
	}//End of DISPLAY FORM OUTCOME

The above code is not working, because if I load the Form, change nothing, and click “Submit”, it always follows the $detailsChanged=TRUE path…

What do I need to do to fix this issue??

Thanks,

Debbie

When the sql executes it will find the right row and update it - whether the information/data has changed or not.
You need to do some comparisons to see if the data has changed.

if($oldvar == $newvar) {
// nothing changed
}
etc

Yeah, I figured it out.

I ran a SELECT and stored all of the record fields in variables and then compared them to the $_POST variables and if there were any changes, then ran my UPDATE.

Thanks anyhow.

Debbie

well done :tup:

Thanks! :blush:

Debbie