Jump to content

Variable not being set as expected.


elmas156

Recommended Posts

Hello everyone,

 

I'm working on an account update page where users can update their email address, password, or security question/answer as desired.  The way I currently have it set up is so that only the information that the user enters will be updated in the database.  So if they only enter a new email address, the email field in the database is updated and all other fields are left as is.

 

I'm also trying to set a javascript even handler (onUnload) depending on whether or not anything was updated.  When their info is successfully updated, I want to alert the user accordingly, as well as when no information was updated.  To do this, I have a variable "$updated" that is set at "false" by default and is supposed to be set to "true" when something is updated and "none" when nothing is updated.  The form and mysql commands are working and the correct fields are being updated as expected.  The problem that I'm having is the $updated variable is not being set correctly and is ALWAYS being set to "true", even when it is expect to be set to "none." Here's the code that I have:

 

<?php
function safe($value){
   	return mysql_real_escape_string($value);
}

$updated = "false";

if (isset($_POST['submit'])) { // If the form has been submitted.

$newemail = $_POST['newemail'];
$pword1 = $_POST['pword'];
$question = safe($_POST['question']);
$answer = safe($_POST['answer']);
$updated = $_POST['updated'];

$pword = md5($pword1);

$q = mysql_query("SELECT * FROM `signin` WHERE email = '$newemail'") or die (mysql_error());
$r = mysql_num_rows($q); // Checks to see if anything is in the db.

if ($r == 1 && $email == $newemail) {

	$noemail = "true";

} elseif ($r == 0) {

	$noemail = "true";

} else {

	$noemail = "false";

}

if ($newemail != "" && $pword1 == "" && $question == "0") {

	if ($noemail == "true") {

		mysql_query("UPDATE `signin` SET `email` = '$newemail' WHERE email = '$email'") or die (mysql_error());

		$_SESSION['email'] = $newemail;

		$updated = "true";

	}

} elseif ($newemail == "" && $pword1 != "" && $question == "0") {

	mysql_query("UPDATE `signin` SET `pword` = '$pword' WHERE email = '$email'") or die (mysql_error());

	$updated = "true";

} elseif ($newemail == "" && $pword1 == "" && $question != "0") {

	mysql_query("UPDATE `signin` SET `question` = '$question',`answer` = '$answer' WHERE email = '$email'") or die (mysql_error());

	$updated = "true";

} elseif ($newemail != "" && $pword1 != "" && $question == "0") {

	if ($noemail == "true") {

		mysql_query("UPDATE `signin` SET `email` = '$newemail',`pword` = '$pword' WHERE email = '$email'") or die (mysql_error());

		$_SESSION['email'] = $newemail;

		$updated = "true";

	}

} elseif ($newemail != "" && $pword1 == "" && $question != "0") {

	if ($noemail == "true") {

		mysql_query("UPDATE `signin` SET `email` = '$newemail',`question` = '$question',`answer` = '$answer' WHERE email = '$email'") or die (mysql_error());

		$_SESSION['email'] = $newemail;

		$updated = "true";

	}

} elseif ($newemail == "" && $pword1 != "" && $question != "0") {

	mysql_query("UPDATE `signin` SET `pword` = '$pword',`question` = '$question',`answer` = '$answer' WHERE email = '$email'") or die (mysql_error());

	$updated = "true";

} elseif ($newemail != "" && $pword1 != "" && $question != "0") {

	if ($noemail == "true") {

		mysql_query("UPDATE `signin` SET `email` = '$newemail',`pword` = '$pword',`question` = '$question',`answer` = '$answer' WHERE email = '$email'") or die (mysql_error());

		$_SESSION['email'] = $newemail;

		$updated = "true";

	} 

} else {

	$updated == "none";

}
}
?>

//This is where the event handler (onUnload) is set according to what has occurred in the code above

<?php
if (isset($_POST['submit']) && $updated = "true") {

echo "<body onUnload=\"alert('Your information has been successfully updated.')\">";


} elseif (isset($_POST['submit']) && $updated = "false") {

echo "<body onUnload=\"alert('There have been no updates made to your account information.')\">";

} else {

echo "<body>";

}
?>

 

Can anyone see a problem with this and why the $updated variable is not being set as expected.  Any ideas on a better way to accomplish what I want to do?  Thanks in advance for anyone's help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.