email didnt update in database after made changes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aquilina
Forum Commoner
Posts: 30
Joined: Wed Sep 21, 2011 1:23 am

email didnt update in database after made changes

Post by aquilina »

Anyone can help me check out whats wrong with my code.. its only update my password but didnt update my email

Below is my code

Code: Select all

<div id="main">
<div class="h3">Account Settings</div>
<div class="mainbox">


<?php
if(isset($_SESSION['usernamejob'])) {
	if(!isset($_POST['modify'])){
		$query = mysql_query("SELECT * FROM `employee_user` WHERE `usernamejob`='".$_SESSION['usernamejob']."'") or die(mysql_error());
		$row = mysql_fetch_array($query);
		?>
		<center>
		<table cellspacing=1 cellpadding=5>
		<tr><td class=listtitle colspan=2><center><span class='title2'></span></center></td></tr>
		<?php
		echo "
		<form method=\"POST\">
		<tr><td class=list align=left>Username</td><td class=list> ".$row['name']."<br></td></tr>
		<tr><td class=list align=left>Current Password</td><td class=list> <input type=\"password\" name=\"current\" maxlength=\"12\"><br></td></tr>
		<tr><td class=list align=left>New Password</td><td class=list> <input type=\"password\" name=\"password\" maxlength=\"12\"><br></td></tr>
		<tr><td class=list align=left>Confirm Password</td><td class=list> <input type=\"password\" name=\"cpassword\" maxlength=\"12\"><br></td></tr>
		<tr><td class=list align=left>E-mail</td><td class=list> <input type=\"text\" name=\"email\" value=\"".$row['email']."\"><br></td></tr>
		<tr><td class=listtitle align=left colspan=2><center><input type=\"submit\" name=\"modify\" value=\"Modify\"></form></td></tr></center>";
	} else {
		$usernamejob = mysql_query("SELECT * FROM `employee_user` WHERE `usernamejob`='".$_SESSION['usernamejob']."'") or die(mysql_error());
		$user = mysql_fetch_array($usernamejob);
		$current = mysql_real_escape_string($_POST['current']);
		$pass = mysql_real_escape_string($_POST['password']);
		$cpass = mysql_real_escape_string($_POST['cpassword']);
		$email = mysql_real_escape_string($_POST['email']);
	        if($current) {
			if($current == $user['password']) {
				if($pass != $cpass) {
					echo "Passwords do not match!";
				} else {
					if(strlen($pass) < 6) {
						echo "Your password must be between 6 and 12 characters!";
					} elseif(strlen($pass) > 12) {
						echo "Your password must be between 6 and 12 characters!";
					} else {
						$usernamejob = mysql_query("UPDATE `employee_user` SET `password`='".$pass."' WHERE `usernamejob`='".$_SESSION['usernamejob']."'") or die(mysql_error());
						session_destroy();
                                                echo "Your changes have been saved.";
					}
				}
			} else {
				echo "Your current password is wrong!";
			}
		} elseif($email == "") {
			echo "Please supply an e-mail!";
		} else {
			$usernamejob = mysql_query("UPDATE `employee_user` SET `email`='".$email."' WHERE `usernamejob`='".$_SESSION['usernamejob']."'") or die(mysql_error());
                        echo "Your changes have succesfully been saved to the database!";
		}
	}
	echo "</fieldset>";
} else {
	echo "You are not logged in!";
}
echo "</td></tr></table>";
?>



</div>
</div>

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: email didnt update in database after made changes

Post by social_experiment »

Code: Select all

UPDATE table
SET
field1 = 'value1', field2 = 'value2'
WHERE
userUniqueField = 'userUniqueValue'
You should update both values at once; currently it's possibly that one of the conditions required for email update isn't being met
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
aquilina
Forum Commoner
Posts: 30
Joined: Wed Sep 21, 2011 1:23 am

Re: email didnt update in database after made changes

Post by aquilina »

so what i gonna to do with my code then?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: email didnt update in database after made changes

Post by social_experiment »

Code: Select all

<?php 
// try
$usernamejob = mysql_query("UPDATE `employee_user` SET `password`='".$pass."', `email` = '" . $email . "' WHERE `usernamejob`='".$_SESSION['usernamejob']."'") or die(mysql_error());
?>
The query above will update both of the columns; before you try this you should clean up the logic of your code a bit. Checking (password == newpassword, if email field contains a valid address, etc) should be done prior to updating the database. The code should follow this these steps (use them a guideline)
1. Accept user input
2. Check user input
A. User input not correct, give errors.
3. User input correct: search and update database
4. Inform user of results of update
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
aquilina
Forum Commoner
Posts: 30
Joined: Wed Sep 21, 2011 1:23 am

Re: email didnt update in database after made changes

Post by aquilina »

solved
Post Reply