Jump to content

Sending login information in an e-mail if user forgets, HELP!


SauloA

Recommended Posts

I'm trying to create a form that will that will send a user their password through an email.  The user is supposed to put their username in the text box and than get an email with their login information.

 

Here's my code.  The problem seems to be this code.  I'd appreciate the help.

 

mysql_select_db($database_connBlog, $connBlog);
$sql="SELECT user_pass, user_email FROM user_tbl WHERE user_sname='".$user_sname."'"; 
$r = mysql_query($sql, $connBlog); 
if(!$r) { 
  $err=mysql_error(); 
  print $err; 
  exit(); 
} 
if(mysql_affected_rows()==0){ 
  print "no such login in the system. please try again."; 
  exit(); 
} 
else { 
  $row=mysql_fetch_array($r); 
  $password=$row["user_pass"]; 
  $email=$row["user_email"]; 

  $subject="your password"; 
  $header="from:[email protected]"; 
  $content="your password is ".$password; 
  mail($email, $subject, $row, $header); 

  print "An email containing the password has been sent to you"; 
}

Here's the form where the usename is inputted.

<form name="forgot" method="post" action="forgot.php">
  <input name="sname" type="text" value="user_sname" size="20"/><br />
  <input type="submit" name="submit" value="submit"/>
  <input type="reset" name="reset" value="reset"/>
</form>

 

If this isn't a good way of sending login information.  Can someone send me in the right dircetion?

The part of my code that says:

 

if(mysql_affected_rows()==0){

  print "no such login in the system. please try again.";

  exit();

 

is working is displaying "no such login in the system. please try again." as it should display if there is an error.

 

I don't know what the error is though.

mysql_affected_rows() only works with queries which actually alter records not SELECT statements. Your code might look more like....

 

<?php
  mysql_select_db($database_connBlog, $connBlog);
  $sql="SELECT user_pass, user_email FROM user_tbl WHERE user_sname='".$user_sname."'"; 
  if (!$r = mysql_query($sql, $connBlog)) {  
    $err=mysql_error(); 
    print $err; 
    exit(); 
  }
  if (!mysql_num_rows()){ 
    print "no such login in the system. please try again."; 
    exit(); 
  } 
  $row=mysql_fetch_array($r); 
  $password=$row["user_pass"]; 
  $email=$row["user_email"]; 

  $subject="your password"; 
  $header="from:[email protected]"; 
  $content="your password is ".$password; 
  if (mail($email, $subject, $row, $header)) { 
    print "An email containing the password has been sent to you"; 
  }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.