我正在为网站构建登录和注册表单,但是注册时遇到问题。 当用户注册时,仅将密码存储在数据库中,则用户名和电子邮件不会存储在数据库中。
这是服务器代码:
$sql = "INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($con, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_password, $param_email);
// Set parameters
$param_username = $username;
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
$param_email = $email;
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: home.php");
} else{
echo "Something went wrong. Please try again later.";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($con);
?>
这是注册表格:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Username" id="username" required value="<?php echo $username; ?>">
<span ><?php echo $username_err; ?></span>
<label for="text">
<i class="fas fa-lock"></i>
</label>
<input type="text" name="email" placeholder="email address" id="email" required value="<?php echo $email; ?>">
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Password" id="password" required value="<?php echo $password; ?>">
<label for="confirm_password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="confirm_password" placeholder="confirm password" id="password" required value="<?php echo $confirm_password; ?>">
<input type="submit" value="submit">
<p>Already have an account? <a href="index.php">Login here</a>.</p>
</form>
答案 0 :(得分:0)
因为在参数设置值之前绑定了参数。绑定参数时,如果您的代码值为$ param_username,$ param_password和$ param_email,则为null。之后,设置参数值。
// Set parameters
$param_username = $username;
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
$param_email = $email;
//Bind Param
mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_password, $param_email);
答案 1 :(得分:0)
尝试一下:
$sql = "INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($con, $sql)){
!empty($username) or die("empty username");
$param_username = $username;
$param_password = password_hash($password, PASSWORD_DEFAULT);
!empty($email) or die("empty email");
$param_email = $email;
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_password, $param_email);
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: home.php");
} else{
echo "Something went wrong. Please try again later.";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($con);