我正在测试php中的新东西,但很难解决我的php代码有什么问题。 所以基本上当我输入正常的注册登录时,我的php会抛出msg“signup = invalid”,即代码中的第18行。请帮忙。
<?php
if (isset($_POST['submit'])) {
include_once 'dbc.php';
$login = mysqli_real_escape_string($conn, $_POST['login']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
//Error handler
//Check for empty fields
if (empty($login) || empty($pwd)) {
header("Location: register.php?signup=empty");
exit();
} else {
//Check if input characters are valid
if (preg_match("/^[a-zA-Z]*$/", $first)) {
header("Location: register.php?signup=invalid");
exit();
} else {
$sql = "SELECT * FROM user WHERE name='$login'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: register.php?signup=nametaken");
exit();
} else {
//hash
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
//Insert user into database
$sql = "INSERT INTO user (user_login, user_pwd) VALUES ('$login', '$hashedPwd');";
mysqli_query($conn, $sql);
header("Location: register.php?signup=success");
exit();
}
}
}
} else {
header("Location: register.php");
exit();
}
答案 0 :(得分:0)
这一行
if (preg_match("/^[a-zA-Z]*$/", $first)) {
应该是
if (!preg_match("/^[a-zA-Z]*$/", $login)) {
由于$首先没有被声明,因为它只包含数字,所以它总是为真!意味着它是否等于