-9

I have been using the same as written. But whenever I click on it, it gives an error

Notice: Undefined index: username in D:\xamp\htdocs\xampp\new\login.php on line 3
Notice: Undefined index: password in D:\xamp\htdocs\xampp\new\login.php on line 4
please enter username and password

My HTML is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="login.php" method="POST">Username:
<input type ='text' name="username" /><br />
password<input type="password" name="password" /><br />
<input type="submit" value="login" /></form>
</body>
</html>

While my PHP is:

<?php

$username = '$_POST[username]';
$password = '$_POST[password]';
if($username && $password)
{
$connect = mysql_connect(“localhost”, “root”, “”); 
$query=mysql_query("SELECT * FROM usres WHERE username=$username");
$numrows = mysql_num_rows($query);
if (!connect) { 
die('Connection Failed: ' . mysql_error()); 
}
   mysql_select_db(“phplogin”, $connect);

}else{
    die("please enter username and password");



    enter code here

}


?>
  • 3
    `$username = '$_POST[username]';` remove the quotes and for the other one and also `[username]'` add the quotes in there `['username']'`. Then change the curly quotes `“ ”` to `"` then quote this `$username` in your `where` clause; it's a string. – Funk Forty Niner Feb 09 '15 at 16:30
  • http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – SubjectCurio Feb 09 '15 at 16:31
  • 1
    It's a little bit depressing that I still have to keep this open in a tab all the time : http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php ... and you're vulnerable to SQL injection attacks ... and you seem to have some "fancy" quotes going on `mysql_connect(“localhost”, “root”, “”); ` – CD001 Feb 09 '15 at 16:32
  • 1
    No error reporting/checking done whatsoever. You won't even connect to DB in the first place, not with the quotes you're using, but produce a 500 error. – Funk Forty Niner Feb 09 '15 at 16:35
  • 1
    `['username']'` => `['username']` extra quote on my part; *my bad*. – Funk Forty Niner Feb 09 '15 at 16:39
  • Then we have a possible typo for your table name `usres` which most likely should be `users`. Lordie.... it just keeps getting better, *doesn't it?!* - I could have easily put in an answer for this, but decided not to; I have my reasons. – Funk Forty Niner Feb 09 '15 at 16:44
  • is usres really a table in db? – Atilla Arda Açıkgöz Feb 01 '16 at 15:40

1 Answers1

1

Try this one

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$connect = mysql_connect("localhost", "root", ""); 
if (!$connect) { 
    die('Connection Failed: ' . mysql_error()); 
}
mysql_select_db("phplogin", $connect);

if($username && $password) {
    $query=mysql_query("SELECT * FROM usres WHERE username='".$username."' ");
    $numrows = mysql_num_rows($query);
    if($numrows > 0){
        //redirect to other page
    }else{
        echo "please enter username and password";
    }
} else {
    echo "please enter username and password";
}

?>
jay.jivani
  • 1,560
  • 1
  • 16
  • 33
  • 1
    [Seems like I did all the "explaining" for you already.](http://stackoverflow.com/questions/28414521/undefined-index-username-in-xampp#comment45162321_28414521). *"Try this"* type of answers aren't worth their weight in gold, I can tell you that. – Funk Forty Niner Feb 09 '15 at 16:55