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:you@yourdomain.com"; 
  $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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:you@yourdomain.com"; 
  $content="your password is ".$password; 
  if (mail($email, $subject, $row, $header)) { 
    print "An email containing the password has been sent to you"; 
  }
?>

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.