Jump to content

A simple question with mysql


Killer1390

Recommended Posts

Well, basically I am making my log in script. This is my first website with php and MySQL... actually, its my first website I have done that isn't for a school project.

 

Anywho, the problem I am having is I can't seem to verify the password of the account I am trying to log into.

 

Here is the code snippet I am having trouble with:

$usr = $_REQUEST['Username'];
$pass = $_REQUEST['Password'];
$pass = md5($pass);
if(mysql_query('SELECT Password FROM Accounts WHERE Username = "' .$usr . '"') == $pass)
{
session_start();
$_SESSION['loggedin'] = yes;
$_SESSION['User'] = $usr;
$_POST['info'] = ("You have successfully logged in " . $usr . ".");
} else { $_POST['info'] = "Username and password do not match.";}

 

The problem is that it doesn't seem to matter if the username and password are correct, it always prints "Username and password do not match.".

 

So, here is the table layout of 'Accounts':

thing.png

 

Did I type the mysql query wrong?

 

 

 

 

 

Link to comment
Share on other sites

You're most of the way there, check this against your and see if you can get it to work:

 

$result = mysql_query("SELECT password FROM Accounts WHERE Username = '".$_POST['Username']."'") or die(mysql_error());			
if(mysql_num_rows($result) != 1) { echo "No such user exists"; unset($_POST); exit(); }
if(md5($_POST['Password']) == mysql_result($result, 0, "Password"))
{
$_SESSION['loggedin'] = yes;	
$_SESSION['User'] = $usr;
$_SESSION['message']= "You have successfully logged in " . $usr;
} else { 
$_SESSION['message'] = "Username and password do not match.";
UNSET($_POST);
}

 

Try not to use $_REQUEST, it's better to use $_POST and $_GET

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.