gpt4 book ai didi

php - 为什么我的最后两个变量没有插入到数据库中?

转载 作者:行者123 更新时间:2023-11-30 21:34:34 25 4
gpt4 key购买 nike

出于某种原因,我的最后两个变量不会输入到数据库中。我指的是 $verifyKey$keyExpire。这是我的代码和评论。我正在添加整个页面以确保我没有在不应该出现的地方出现错误的字符。实际上,这是一个注册页面,将信息插入数据库并为以后的电子邮件验证提供验证 key 。

我将字段与数据库中的代码匹配,并将它们设置为 longtext 和 text。我不想直接插入,因为我试图让此方法适用于所有 5 个变量。

<?php

// This is not seen by the end user so this file is placed in the unseen folder
// Check if the user used the sign up button


if (isset($_POST['signup-submit'])) {
// uses the database handler
require 'dbh.php';
$username=$_POST['uid'];
$email=$_POST['mail'];
$password=$_POST['pwd'];
$passwordcnfrm=$_POST['pwd-cnfrm'];
$verifyKey=md5(time().$username);
$keyExpire=date("U")+ 86400;


// Checks to see if any field are empty
if(empty($username)||empty($email)||empty($password)||empty($passwordcnfrm)) {

// This header returns the username and/or email address so the user doesn't have to retype it
header("Location:../signup.php?error=emptyfields&uid=".$username."&mail=".$email);

exit();
}
// Checks if both the user and email are invalid
else if (!filter_var($email, FILTER_VALIDATE_EMAIL)&&!preg_match("/^[a-zA-Z0-9]*$/",$username)) {
header("Location:../signup.php?error=invalidmailuid");
exit();
}
// Checks if the email is valid
else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location:../signup.php?error=invalidmail&uid=".$username);
exit();
}
// Checks if the username is valid.
else if (!preg_match("/^[a-zA-Z0-9]*$/",$username)) {
header("Location:../signup.php?error=invaliduid&mail=".$email);
exit();
}
// Checks to see if the password and confirm password match
else if ($password !== $passwordcnfrm){
header("Location:../signup.php?error=passwordcheck&uid=".$username."&mail=".$email);
exit();
}
// Checks to see if the username is already in use or password is invalid
else {

$sql = "SELECT uidUsers FROM users WHERE uidUsers=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt,$sql)) {
header("Location:../signup.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt,"s",$username);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows();
if ($resultCheck < 0){
header("Location:../signup.php?error=usertaken&mail=".$email);
exit();
}
else {
//Inserts into database

$sql = "INSERT INTO users (uidUsers,emailUsers,pwdUsers,verify,expires) VALUES (?,?,?,?,?);";
$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt,$sql)) {
header("Location:../signup.php?error=sqlerror");
exit();
}
else {
$hashedPwd =password_hash($password,PASSWORD_DEFAULT);

mysqli_stmt_bind_param($stmt,"sssss",$username,$email,$hashedPwd,$verifyKey,$keyExpire);
mysqli_stmt_execute($stmt);

header("Location:../signup.php?signup=success");

}

}
}
}
//Closes session to db
mysqli_stmt_close($stmt);
mysqli_close($conn);

}
else {
header("Location:../signup.php");
}

最佳答案

mysqli_stmt::bind_param 的描述中有这样的注释:

If data size of a variable exceeds max. allowed packet size (max_allowed_packet), you have to specify b in types and use mysqli_stmt_send_long_data() to send the data in packets.

http://php.net/manual/en/mysqli-stmt.bind-param.php

实际上,这意味着对于每个长文本(和 blob)字段,您必须在 mysqli_stmt_bind_param 之后额外调用 mysqli_stmt_send_long_data。否则,当一切正常但未设置字段时,您会出现这种奇怪的行为。

关于php - 为什么我的最后两个变量没有插入到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54619819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com