?
Solved

PHP if not quite working

Posted on 2007-12-04
7
Medium Priority
?
236 Views
Last Modified: 2013-12-12
Hi i have the following PHP script which , after a bit a tweeking isn't working anymore:

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

   
// QUERY
$query = 'SELECT password FROM users1 WHERE email = \''.$_POST['email'].'\' LIMIT 1';
$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
 

$line = mysql_fetch_array($result, MYSQL_ASSOC);
if (!$line) {
    $error = "Email Not Found in Database";
    require_once("forgetpass.php");
}else{  

After an email address has been found the script moves on to Mail the user the password.

What i get is the error "Not Found in Database", even when i know there is an email address.

Where have i gone wrong?

Hope someone can advise...

Anthony
0
Comment
Question by:d10u4v
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20402587
try print the email before the query to see it is send.

print $_POST['email'];
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20402608
You should also use mysql_real_escape_string in any form values

$query = 'SELECT password FROM users1 WHERE email = \''.mysql_real_escape_string($_POST['email']).'\' LIMIT 1';

and you can use mysql_fetch_assoc instead of mysql_fetch_array($result, MYSQL_ASSOC);

$line = mysql_fetch_assoc($result);
0
 

Author Comment

by:d10u4v
ID: 20402609
the email bit works fine - the only section i altered is the code i included above.  To me it looks as though it should work, but i'm no expert.... ;)

Anthony
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:DrNikon224
ID: 20402640
Change password to `password` as it is a reserved word in MySql. It's a good idea to use backticks on all of your table and field names. It's even better to avoid using reserved words as field/table names.
$query = 'SELECT `password` FROM `users1` WHERE `email` = \''.$_POST['email'].'\' LIMIT 1';

Open in new window

0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 20402645
we know that there are no sql error because

$result = mysql_query($query) or die ('Query failed: ' . mysql_error());

would end the script.

Also there are no php errors or you would get a warning.

So the only options left are ...

either $_POST["email"] is not set ... i.e you could be sending the form via get ... in which case you would use $_GET["email"]

or your table is incorrect ...

i noticed that you are selecting from table users1 perhaps the table with the records is table users ??

0
 

Author Comment

by:d10u4v
ID: 20402693
I'M SO STUPID!

As soon as you pointed out the $_post and $_get, i look at my form and readlised that i had not renamed my textfield.  As soon as i saw 'email' in you post i realised what i had done, or not done.

Sorry to waste time.

Thank you anyway.

Anthony
0
 

Author Closing Comment

by:d10u4v
ID: 31412574
It's not the answer beu i got me looking inthe right place, and made me realise my error.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question