PHP无法从数据库检索数据

时间:2019-12-22 16:01:54

标签: php mysqli prepared-statement

我是PHP的新手,我正尝试通过用户名和密码对用户进行身份验证。(基本上是登录表单)如果密码匹配,则会将其重定向到welcome.php。我遇到两个问题。主要的问题是尽管代码Warning: mysqli_stmt::bind_param(): Number of variables doesn't match the number of parameters in prepared statement有四个参数,但我仍然收到警告$stmt->bind_param("ssss", $id, $username, $password, $data);。第二个问题是仅在密码匹配时如何重定向用户。如何解决这两个问题?

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); 

在config.php中,我有:

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.

0 个答案:

没有答案