Help with Password Retrieval Code

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
grozanc
Forum Newbie
Posts: 14
Joined: Tue Oct 23, 2007 6:50 pm

Help with Password Retrieval Code

Post by grozanc »

Hello,

Of course I'm a newbie or I wouldn't need to ask this but I'm having trouble getting the following code to work. However, it does work until I include the bit of code

Code: Select all

if (mysql_num_rows($result) > 0)
{
and

Code: Select all

else
{
echo "no records!";
}
Can anyone point out where my error is at? Thanks in advance!

Code: Select all

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form method="POST" action="">
Search User: <input type="text" name="email">
<input type="SUBMIT" value="Search!">
</form>

<?
include 'library/config.php';
include 'library/opendb.php';

$email=$_POST['email'];

$sql = mysql_query("SELECT firstname, email, password FROM stories WHERE email = '$email'") or die (mysql_error());

$result = mysql_query($query) ;

if (mysql_num_rows($result) > 0)
{ 

while(list( $firstname, $email, $password)=mysql_fetch_array($sql)){

//change this to your email.
    $to = "$email";
    $from = "info@pavementmemors.com";
    $subject = "Your Lost Pavement Memoirs Username & Password";
    //begin message
    $message = <<<EOF
<html>
<body>
Hello $firstname,<br>
<br>
Here is your password and email.
<br><br>
Username: $email<br>
Password: $password<br>
<br>
Keep in mind each time you edit your post, the new submission will not be viewable until it&rsquo;s reviewed to keep the site PG rated.
<br><br>
Sincerely,<br>
Gary
</body>
</html>
EOF;
   //end of message
    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    $to = "$to, $from"; // this will send to both emails at the same time.
    // now lets send the email.
    mail($to, $subject, $message, $headers);

echo "Results sent to: $email<br />";

}

else
{
echo "no records!";
} 
?>
</body>
</html>
The working code if it helps:

Code: Select all

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form method="POST" action="">
Search User: <input type="text" name="email">
<input type="SUBMIT" value="Search!">
</form>

<?
include 'library/config.php';
include 'library/opendb.php';

$email=$_POST['email'];

$sql = mysql_query("SELECT firstname, email, password FROM stories WHERE email = '$email'") or die (mysql_error());

$result = mysql_query($query);

while(list( $firstname, $email, $password)=mysql_fetch_array($sql)){

//change this to your email.
    $to = "$email";
    $from = "info@pavementmemors.com";
    $subject = "Your Lost Pavement Memoirs Username & Password";
    //begin message
    $message = <<<EOF
<html>
<body>
Hello $firstname,<br>
<br>
Here is your password and email.
<br><br>
Username: $email<br>
Password: $password<br>
<br>
Keep in mind each time you edit your post, the new submission will not be viewable until it&rsquo;s reviewed to keep the site PG rated.
<br><br>
Sincerely,<br>
Gary 
</body>
</html>
EOF;
   //end of message
    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    $to = "$to, $from"; // this will send to both emails at the same time.
    // now lets send the email.
    mail($to, $subject, $message, $headers);

echo "Results sent to: $email<br />";

}
?>
</body>
</html>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Next time please use tags when posting php code.

As for your problem, look carefully here

Code: Select all

$sql = mysql_query("SELECT firstname, email, password FROM stories WHERE email = '$email'") or die (mysql_error());

$result = mysql_query($query) ;

if (mysql_num_rows($result) > 0)
{
For some reason, you have a second mysql_query(), in which $result is not a valid query string either. You can remove this line and change $sql to $result and things should work fine.

You should also be escaping your input variables input the query string, mysql_real_escape_string(), else you'll be vulnerable to sql injection
grozanc
Forum Newbie
Posts: 14
Joined: Tue Oct 23, 2007 6:50 pm

Didn't work?

Post by grozanc »

Thanks for the reply and I'll be more careful with the php code function.

Anyway, I tried your suggestion and it didn't work, well I didn't undestand how to make it work. As soon as I add in the if statement I get an empty page. Anyother advice for this newbie? Below is the revised yet still nonworking code. Again, keep in mind it works until I add in the

Code: Select all

if (mysql_num_rows($result) > 0)
{

Code: Select all

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form method="POST" action="">
Search User: <input type="text" name="email">
<input type="SUBMIT" value="Search!">
</form>

<?
include 'library/config.php';
include 'library/opendb.php';

$email=$_POST['email'];

$result = mysql_query("SELECT firstname, email, password FROM stories WHERE email = '$email'") or die (mysql_error());

if (mysql_num_rows($result) > 0)
{ 

while(list( $firstname, $email, $password)=mysql_fetch_array($result)){

//change this to your email.
    $to = "$email";
    $from = "info@pavementmemors.com";
    $subject = "Your Lost Pavement Memoirs Username & Password";
    //begin message
    $message = <<<EOF
<html>
<body>
Hello $firstname,<br>
<br>
Here is your password and email.
<br><br>
Username: $email<br>
Password: $password<br>
<br>
Keep in mind each time you edit your post, the new submission will not be viewable until it&rsquo;s reviewed to keep the site PG rated.
<br><br>
Sincerely,<br>
Gary Rozanc
</body>
</html>
EOF;
   //end of message
    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    $to = "$to, $from"; // this will send to both emails at the same time.
    // now lets send the email.
    mail($to, $subject, $message, $headers);

echo "Results sent to: $email<br />";

else
{
echo "no records!"; 

}
?>

</body>
</html>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Where do you close the while loop ?

Also, although I know it is a lot to take in, it is a good idea if you use the full tags so use <?php rather than just <?. Not only is it more readable, <? relies on a php.ini setting which may not be available.
grozanc
Forum Newbie
Posts: 14
Joined: Tue Oct 23, 2007 6:50 pm

Still not working!

Post by grozanc »

Obviously I don't know enough to get this working but I want to thank everyone for there help so far. If anyone can show me the actual working code that would be great because obviously at this point that's the level of hand holding I need! :oops:
Post Reply