database doesn't update record

Codes here !

Moderators: egami, macek, gesf

Post Reply
JohnPringle83
New php-forum User
New php-forum User
Posts: 8
Joined: Tue May 03, 2011 5:56 am

Tue May 03, 2011 4:59 pm

Hello, I have created a page where users can request a new password using just their e-mail address.

The if($query) must be true because I do not get the error message associated with it and a new password is generated and displayed on screen along with the username. However, when I check my database, the field is not updated. I have tried rearranging syntax and looked on php.net for their examples of syntax. I just can't seem to figure out what the problem is.

Eventually I want the code to email the user the new password, but I will come to that later, for now I'm displaying the password back to the user in their browser

I just want to make sure that the password updates in the database.

Please could anyone help me find my problem?

Here is the code:

Code: Select all

<?php require_once("includes/connection.php"); ?>
<?php include_once("includes/header.php")?>
<?php require_once("includes/formfunctions.php"); ?>

<?php
	$message="Enter your registered e-mail to reset your password";
	$invalid="Your email does not exist in our database";
	$success="Your new password has been emailed to you";
	
	if(isset($_POST['submit'])){

		//Error checks
		$errors = array();
			
		if (empty($_POST['email'])){
			$errors[] = "Enter your email";
		}
		
		if (empty($errors)) {

			$email = $_POST['email'];

			$sql1="SELECT * FROM users WHERE email='$email'";
			$result1=mysql_query($sql1);
			$count=mysql_num_rows($result1);
			
			if($count==1){
				
				$rows=mysql_fetch_array($result1);
				$username=$rows['username'];
				
				//generate new password
				$password=rand(100000, 999999);
				$codedpassword=md5($password);
				
			$query = "UPDATE users SET password='$codedpassword' WHERE email='$email'";
				if($query){
					$message=$success."<br /><br />";
					$message.="Your username is:<br />{$username}<br /><br />";
					$message.="Your new password is:<br />{$password}";
				}else{
					$message="There was a problem getting a new password<br />
					Please use the old one";
				}
			}else{
				$message=$invalid;
			}
		}else{
			$message=$invalid;
		}
	}
		
	if (empty($errors)) {
		echo "<h4 align=\"center\">{$message}</h4>";
	}
	
	if (!empty($errors)) {
		display_errors($errors);
	}
?>

<form action="forgotlogin.php" method="post">
	<table align="center">
		<tr>
			<td>Email:</td>
			<td><input type="text" name="email" /></td>
		</tr>
		<tr>
			<td><a href="login.php">Cancel?</a></td>
			<td><input type="submit" value="Get Details" name="submit" /></td>
		</tr>
	</table>
</form>
<?php include_once("includes/footer.php")?>

JohnPringle83
New php-forum User
New php-forum User
Posts: 8
Joined: Tue May 03, 2011 5:56 am

Tue May 03, 2011 5:35 pm

I have fixed this problem

I had forgotten to run the mysql_query() function on the query and then instead of testing the query if it was true, I should've tested to see if the result was true.

so inbetween these two lines of code:

Code: Select all

$query = "UPDATE users SET password='$codedpassword' WHERE email='$email'";
            if($query){
I placed this line of code:

Code: Select all

$result=mysql_query($query);
and changed:

Code: Select all

if($query){
to:

Code: Select all

if($result){

Post Reply