MySQL WHERE syntax using POST variable

I am trying to write an SQL statement that fetches the value in the "userkey" field when the "username" and "userpassword" are equal. The "username" and "userpassword" are contained in variables passed using $_POST. The statement follows:
$sql = "SELECT userkey FROM tbl_name WHERE (username = ".$_POST['username'].") AND (userpassword = ".$_POST['password'].")";

Assuming the username is "John", the results of the above query is:
Unknown column 'john' in 'where clause'

Might someone correct my syntax to yield the desired results: the userkey?
Thanks.
lepirtleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

maeltarCommented:
Firstly you should always use some method of preventing SQL Injection, no matter how simple..

$uname = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);

$sql = "SELECT userkey FROM tbl_name WHERE username = '{$uname}' AND userpassword = '{$pass}'");

Open in new window


try that

Regards

S
0
eriksmtkaCommented:
0
lepirtleAuthor Commented:
Hi Maeltar,
I appreciate, and will incorporate your Injection statements.

I tried your revised sql statement and initially received an error:
Parse error: syntax error, unexpected ')'

So I removed the last ')' but now, when I echo the $userkey I receive the word "Array'.

$uname = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);

$sql = "SELECT userkey FROM tbl_name WHERE username = '{$uname}' AND userpassword = '{$pass}'";

$result_set = mysql_query($sql) or die(mysql_error());

$userkey = mysql_fetch_array($result_set);

echo $userkey;

Am I wrong in using the following to display the userkey?

 $userkey = mysql_fetch_array($result_set);
 echo $userkey;

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Dave BaldwinFixer of ProblemsCommented:
Here http://www.phpeasystep.com/workshopview.php?id=6 is a script similar to one I use.  Note the addition at the bottom of the page about encrypting the password.  It is generally considered bad to store passwords in plain text in your database.
0
maeltarCommented:
Sorry about the ')'  I should have double checked before I hit Submit

$userkey will be an array...

try

$uname = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);

$sql = "SELECT userkey FROM tbl_name WHERE username = '{$uname}' AND userpassword = '{$pass}'";

$result_set = mysql_query($sql) or die(mysql_error());

$userkey = mysql_fetch_array($result_set);

echo $userkey[0];

print_r($userkey);

Open in new window


I would suggest using "mysql_fetch_row"

Or if you are just wanting to see if it comes back with > 0 results..

$uname = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);

$sql = "SELECT userkey FROM tbl_name WHERE username = '{$uname}' AND userpassword = '{$pass}'";

$result_set = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result_set) > 0)
{
// code in here you have a match
}else{
// code in here for no matches
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lepirtleAuthor Commented:
Thanks Malter. That did the trick!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.