我对PHP很陌生。为什么我在以下位代码上出现内部错误:
// DB connection (working)
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$arr = array(); // array for errors later used with http_build_query
解析用户名:
$username = test_input($_POST["myusername"]);
if (!preg_match("/^[a-zA-Z ]*$/", $username)) {
$arr["userErr"] = "1"; // Invalid username!
} else {
$sql = "SELECT * FROM $tbl_name WHERE username = '$username';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$arr["userErr"] = "2"; // Username already registered!
} else {
$arr["userErr"] = "0"; // no problems
$ok++;
}
}
解析密码:
$password = test_input($_POST["mypassword"]);
if (strlen($password) < 6) {
$arr["passErr"] = "1"; // Password is too short!
} else if (strlen($password) > 30) {
$arr["passErr"] = "2"; // Password it too long!
} else {
$arr["passErr"] = "0"; // no problems
$ok++;
}
解析电子邮件:
$email = test_input($_POST["myemail"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$arr["emailErr"] = "1"; // Invalid email format
} else {
$sql = "SELECT * FROM $tbl_name WHERE email = '$email';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$arr["emailErr"] = "2"; // Email already registered!
} else {
$arr["emailErr"] = "0";
$ok++;
}
}
如果一切顺利,我们会将数据发送到数据库(如果$ ok == 3)并将用户发送回索引并成功发送消息(位于index.php中):
if($ok == 3) {
$sql = "INSERT INTO $tbl_name (username, password, email)
VALUES ('$username', '$password', '$email');";
$conn->query($sql);
header("location:index.php?" . "good=true");}
否则,我们将用户发送到索引,并显示包含警告信息的错误消息:
} else {
header("location:index.php?" . http_build_query($arr));
}
}
答案 0 :(得分:0)
问题解决了!感谢@u_mulder和@ chris85指出错误,也终于意识到我使用了错误的连接方法,这是问题的一部分。
$ conn应该是mysql_query