-3

Here is my php login code where i have problem with my condition. I think.

<?php


session_start();

$servername = "localhost";
$username = "root";
$password = "";
$mysql_database = "practiceLogin";


$conn = mysqli_connect($servername, $username, $password) or die ("Connection failed: " . mysqli_connect_error());
mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");




if (isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['Password'];

$stmt = $conn->prepare("SELECT * FROM tbl_account WHERE uName = ? AND uPassword = ?");
$stmt->bind_param('ss',$username,$password);
$stmt->execute();
$get_result=$stmt->get_result();
$row_count= $get_result->num_rows;

$stmt->close();
$conn->close();

if ($row_count>0){
$_SESSION['User_name'] = $username;


echo "Login Success!";
exit();
}else{
echo "Wrong username and password";
exit();
}

}

?>

<html>
<head>
<title>Login</title>
</head>

<body>
<form action = "?" method = "POST">
<input type = "text" name="username"><br>
<input type = "password" name = "password"><br>
<input type = "submit" name = "submit" value = "Login"></br>
<a href = "register.php">Create Account</a>
</form>
</body>
</html>

Everytime I login to this i always go to the else statment which is Wrong username and password always showing up even i have the right credentials? What am I missing?

I don't need to hash my password at this time i just need an answer if i can have a successful login page. Thanks

Jeyjey
  • 341
  • 1
  • 4
  • 10
  • 2
    possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner Jul 01 '16 at 02:22
  • 3
    and this question's a repost of your other question http://stackoverflow.com/questions/38112958/why-my-php-code-is-not-working where you made the same error. – Funk Forty Niner Jul 01 '16 at 02:23
  • 1
    Passwords should be hashed. – chris85 Jul 01 '16 at 02:27

2 Answers2

1

Hi use have given name to your password field to name = "password". But after submit you have been used $password = $_POST['Password'];. Use$_POST['password']. Thats why you are not getting any result.

Also you can select database in single line like

$conn = mysqli_connect($servername, $username, $password,$databasename);

then you don't need to use mysqli_select_db();

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0

Update your php part & uncomment the commented line so that you can get the exact features:

if (isset($_POST['submit'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $conn->prepare("SELECT * FROM tbl_account WHERE uName = ? AND uPassword = ?");
    $stmt->bind_param('ss',$username,$password);
    $stmt->execute() or die($stmt->error);
    $get_result=$stmt->get_result();
    //print_r($get_result); //DEBUG purpose 
    $row_count= $get_result->num_rows;
   //print_r($row_count); die;// DEBUG purpose 
    $stmt->close();
    $conn->close();

   if ($row_count>0){
       $_SESSION['User_name'] = $username;
       echo "Login Success!";
       exit();
   }else{
      echo "Wrong username and password";
       exit();
   }

}
Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14
  • `mysqli_result Object ( [current_field] => 0 [field_count] => 10 [lengths] => [num_rows] => 1 [type] => 0 ) 0` this is whats happening is this alright? @Dipanwita Kundu – Jeyjey Jul 01 '16 at 11:56