Hello, I trying to learn how to create a forgot password page. I used some code from another forum and having some trouble with it. I get an error for the $recs and $row variables. Any feedback on this topic would be appreciated. Thank you.

<?php require_once('Connections/connadmin.php'); ?>
<?php
session_start();  // Start Session
session_register("session");
// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
// Convert to simple variables  
$email_address = $_POST['email_address'];
if (!isset($_POST['email_address'])) {
?>

<?php
}
elseif (empty($email_address)) {
    echo $empty_fields_message;
}
else {
$email_address=mysql_real_escape_string($email_address);
$status = "OK";
$msg="";
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
if (!stristr($email_address,"@") OR !stristr($email_address,".")) {
$msg="Your email address is not correct<BR>"; 
$status= "NOTOK";}

echo "<br><br>";
if($status=="OK"){  $query="SELECT e_mail,user_name FROM register WHERE register.e_mail = '$email_address'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$em=$row->email_address;// email is stored to a variable
 if ($recs == 0) {  echo "<center><font face='Verdana' size='2' color=red><b>No Password</b><br> Sorry Your address is not there in our database . You can signup and login to use our site. <BR><BR><a href='http://www.jackgodfrey.org.uk/register'>Register</a> </center>"; exit;}
function makeRandomPassword() { 
          $salt = "abchefghjkmnpqrstuvwxyz0123456789"; 
          srand((double)microtime()*1000000);  
          $i = 0; 
          while ($i <= 7) { 
                $num = rand() % 33; 
                $tmp = substr($salt, $num, 1); 
                $pass = $pass . $tmp; 
                $i++; 
          } 
          return $pass; 
    } 
    $random_password = makeRandomPassword(); 
    $db_password = md5($random_password); 

    $sql = mysql_query("UPDATE register SET password='$db_password'  
                WHERE e_mail='$email_address'"); 

    $subject = "Your password at www.yoursite.com"; 
    $message = "Hi, we have reset your password. 

    New Password: $random_password 

    [url]http://www.yoursite.com/login[/url]
    Once logged in you can change your password 

    Thanks! 
    Site admin 

    This is an automated response, please do not reply!"; 

    mail($email_address, $subject, $message, "From: yoursite.com Webmaster<admin@jyoursite.com>\n 
        X-Mailer: PHP/" . phpversion()); 
    echo "Your password has been sent! Please check your email!<br />"; 
    echo "<br><br>Click <a href='http://www.yoursite.com/login'>here</a> to login";
 } 
 else {echo "<center><font face='Verdana' size='2' color=red >$msg <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}
}
?> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Frogot Password</title>
</head>

<body>
<h2>Recover a forgotten password!</h2>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <p class="style3"><label for="email_address">Email:</label>
    <input type="text" title="Please enter your email address" name="email_address" size="30"/></p>
    <p class="style3"><label title="Reset Password">&nbsp</label>
    <input type="submit" value="Submit" class="submit-button"/></p>
</form>
<?php
echo $recs, $row;
?>

</body>
</html>

There is a problem with your query, check your database.. Maybe the field-names are incorrect or your table name

$query="SELECT e_mail,user_name FROM register WHERE register.e_mail = '$email_address'"

Thanks, incorrect table name. I fixed it but still having same problem. Could this be because I am still using a testing server?

If you've set up your database correctly on your test server, then it should work. What error msg do you see?

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

I have this for both lines for variables $recs and $row,

Try this and post the output..

$st=mysql_query($query);
if ($st)
{
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
}
else
{
echo "Error in query: ".mysql_error();
}

Thanks, I try that too, I seem to have got the changing password to work! Now I'm getting an email error:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in

Any feedback on that would be great, as I said I am new to working with this part. Thanks again for the help, it's slowly coming together.

You're trying to send a mail using SMTP. Your currently settings says you have a local mailserver. As this is your test-server, I don't think you have a mail server. You can modify your php.ini file to set a remote smtp server (most isp have a smtp server.
Modify this part:
[mail function]
SMTP = smtp.isp.net

Or you can do it from your current php using ini_set() function:

ini_set ( "SMTP", "smtp.isp.net" );

Remember to change smtp.isp.net with the one of you ISP

Great, Thanks again.

sir where i have to place the smtp=smtp.isp.net i dont understand.could you please explain it briefly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.