gpt4 book ai didi

php - 通过 PHP 插入 MySQL 数据库只工作了一半

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

所以我目前正在学习 PHP,并且正在使用 POST 方法创建一个带有注册表单的简单 PHP 页面。在表单提交时,页面对密码进行哈希处理(使用 phpass),验证用户名是否有效(也就是说,它当前不存在于数据库中)并在为真时插入。我的代码正在插入新行,但我没有看到存储的用户名或哈希值的值。这是 PHP:

require("PasswordHash.php");
$unSuccess = false;
$pwSuccess = false;
$registerSuccess = false;
$spamSuccess = false;

$database = "XXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXX";
$server = "XXXXXXX";
$db = new mysqli($server, $username, $password, $database);

$user = "";
$pass = "";


if (mysqli_connect_errno())
{
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}


if($_POST["usr"] && !$unSuccess){
$un = $_POST["usr"];

if(strlen($un) < 20){
//Verify Username is valid
if(preg_match("/([A-Za-z0-9])/", $un) == 1){
//Username is valid, check if it already exists in db.
$unCheckQuery = "SELECT USERS.Username FROM USERS WHERE USERS.Username = '$un'";
$result = $db->query($unCheckQuery);
$num = $result->num_rows;
$result->close();
if($num != 0){ $errUsername = "Username already exists."; $unSuccess = false; }
}
else{
//Username is valid and not taken
$user = $un;
$unSuccess = true;
}
}
}


if($_POST["password"] && !$pwSuccess){
//verify and hash pw
$pw = $_POST["password"];
if(str_len($pw) > 72){die("Password must be shorter than 72 characters");}
$hasher = new PasswordHash(8, false);
$hash = $hasher->HashPassword($pw);
if(strlen($hash) >= 20 && preg_match($pattern, $pw) == 1){
$pass = $hash;
echo $pass;
$pwSuccess = true;
}
else{
$pwSuccess = false;
}
}
if($_POST["spam"]){
$s = $_POST["spam"];
if($s != 10){
$spamSuccess = false;
}
else if($s == 10) {$spamSuccess = true;}
}

if($unSuccess = true && $pwSuccess = true && $spamSuccess = true){
$registerQuery = "INSERT INTO USERS(Username, phash) VALUES('$user', '$pass')";
//This line is breaking evrything.
$db->query($registerQuery);

}

我使用的表单是一个简单的 HTML 表单。出于显而易见的原因,我省略了登录信息。任何指向正确方向的指示都将不胜感激!

最佳答案

if($unSuccess = true && $pwSuccess = true && $spamSuccess = true)

需要

if($unSuccess == true && $pwSuccess == true && $spamSuccess == true)

关于php - 通过 PHP 插入 MySQL 数据库只工作了一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39712383/

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