I have the following two php scripts:
config.php:
<?php
define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'mind3');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
and index.php, which contains the login script and form:
<?php
require_once 'config.php';
$username=$password='';
$username_err=$password_err='';
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["username"]))){
$username_err = 'Please enter an username!';
} else{
$password = trim($_POST['username']);
}
if(empty(trim($_POST["password"]))){
$pasword_err = "Please enter a password!";
} else{
$password = trim($_POST['password']);
}
if(empty($username_err) && empty($password_err)){
$sql = "SELECT username,password FROM members WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_username);
$param_username = $username;
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
if(password_verify($password, $hashed_password)){
session_start();
$_SESSION['username'] = $username;
header("location: welcome.php");
} else{
$username_err = 'Username/password is wrong!';
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
}
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="wrapper">
<h2>Log in</h2>
<form action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo(!empty($username_err)) ? 'has-error' : ''; ?>">
<input type="text" name="username" class="form-control" placeholder="Username">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<input type="password" name="password" class="form-control">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Login">
</div>
</form>
</div>
</body>
</html>
The problem is that whenever I input the credentials I have in my database, it does not redirect me to welcome.php, and it also doesn't tell me that the password or username is wrong.But when i don't input username or password I do get "please enter username" or "please enter password".
I don't understand what is wrong.