问题
I'm new to PHP and I'm trying to authenticate the users by their username and password.(basically login form) If their password matches then they are redirected to welcome.php. I'm experiencing two issues. The major one is that I receive a warningWarning: mysqli_stmt::bind_param(): Number of variables doesn't match the number of parameters in prepared statement despite the code $stmt->bind_param("ssss", $id, $username, $password, $data);, which has 4 parameters. The second question is how to redirect the user only when the password is matched. How to solve these two problems?
session_start();
require_once "config.php";
$username = $_POST['username'];
$password = $_POST['password'];
$conn=$link;
$query = "SELECT id, username, password, data FROM mathbglogin WHERE username = '$username' and password = '$password'";
$stmt = $conn->prepare($query);
$stmt->bind_param("ssss", $id, $username, $password, $data);
$stmt->execute();
echo $id;
$stmt->bind_result($id, $username, $password, $data);
if($stmt->fetch())
$_SESSION['username'] = $username;
$_SESSION['data'] = $data;
// header("location: welcome.php"); I marked this like because it is always executed, which should be changed to executed only when the user is authenticated
echo $username;
mysqli_close($conn);
In config.php I have:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'users');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
This is the database screenshot. I have dummy data in it.
来源:https://stackoverflow.com/questions/59445805/php-unable-to-retrieve-data-from-database