tags:

views:

286

answers:

10

Okay I have two variables in PHP

$username;
$password;

which are initialized to the data retrieved from $_POST variable :)

I have this SQL query

$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $password . "')";

But this doesn't works and returns me nothing :(

Can you instruct me into the right direction. Please?

+4  A: 

eeek! sql injection for one!

EDIT: http://stackoverflow.com/questions/84556/whats-your-favorite-programmer-cartoon/84629#84629

IainMH
Without looking, I will assume this is the Little Bobby Tables cartoon
Joe Philllips
Correctumundo. .
IainMH
+6  A: 

What's wrong with it?

Everything, unfortunately. In particular it's open to SQL injection attacks.

If that's a verbatim cut&paste, then the reason it's not actually working is a trailing closing bracket. Presumably you're not checking for errors when you call this?

Using the base MySQL API it should be:

$sth = $db->prepare("SELECT COUNT(*) FROM users WHERE username = ? AND password = ?");
$sth->execute($username, $password);
list($count) = $sth->fetchrow();
$authorized = ($count > 0);

or similar (code untested, E&OE, etc...)

Alnitak
+10  A: 

The query has a closing parenthesis on the end for no reason, it won't work.

Chad Birch
I didn't see that. Was still reeling from shock of seeing such a security risk. However, this is probably the best answer. Removing the parentheses should make the code work but *please* don't use that in production.
IainMH
+2  A: 

Why is there a stray ) at the end of your query? It shouldn't be there.

Oh, and thirded on SQL injection. BAD.

Matt
A: 

You seem to have an excess closing parenthesis at the end of your query string.

[Edit] - for those screaming SQL injection attacks: we don't know what the user has done with their variables before using them in the query. How about benefit of doubt? ;-)

BrynJ
Safe than sorry, etc.
John Rasch
Better to tell him and find that he's already thought about it than to *not* tell him in case he hasn't.
Graeme Perrow
You should always sanitize the data as it's being passed to the database in addition to validating any input.
Matt
Of course I advocate proper handling of user entered variables. I've updated the smiley in my post for clarity ;-)
BrynJ
+1  A: 

First of all, never, ever do it like this. Please read about SQL injection and don't write any SQL until you have understood what it says. Sorry, but this is really essential.

That said, your query contains a closing bracket. That looks like a syntax error. Do you get an error executing it?

sleske
+7  A: 

Enjoy this:

Quassnoi
Hahaha, this is always funny..., but why is this the 'chosen' answer. It doesn't answer the question (regardless of how pertinent the suggestion is...).
KyleFarris
It seems @op got an answer to his question :) Worth a thousand words as they say :)
Quassnoi
How can you possibly know he didn't escape his data first? :P
Mario
He accepted the comic as an answer to "what's wrong with the query", that's how :)
Quassnoi
+1  A: 

There's an extra parenthesis on the right hand side of the query.

Also, if you do not sanitize your code properly you're going to be vulnerable to SQL injection. You should really be using parameterized queries, but in lieu of that at least use mysql_real_escape_string() on $username and $password.

Also, as a bit of ghost debugging, it's very possible that your passwords are MD5 hashed in the database, since you should never store them in plain text.

Try:

$username = mysql_real_escape_string($_POST["username"]);
$password = md5($_POST["password"]);

$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
John Rasch
MySQL has its own perfectly good MD5() function...
Alnitak
I don't remember it applying it automagically
John Rasch
A: 

In addition to all the other problems noted. The Password in the Users table is stored encrypted. Unless you've run the Password through the MySQL password encryptor, you will never see any data from this query as the passwords won't match.

Thomas Jones-Low
How can you assume to know how the password is stored in his database schema?
Chad Birch
A: 

Hey guys thank you that solved the problem.

Well, I have filtered the data already before storing it in $username and $password. By filtering, I mean I have applied the mysql_real_escape_string() function to the input.

I just didn't show the whole script here just to make the answering process easier :)

Anyways thanks again both for the answer and for the advice.

hab