我正在尝试使用验证进行注册表单。我在我的html和css代码中添加了一些php代码。一切都是好的。它验证用户或电子邮件是否已存在。如果验证通过,那么数据将保存在数据库中但不起作用。我不知道问题是什么。
这是我的代码:
$username = ($_POST['username'])
首先,我写了$Call = array(
"Foo1" => array(
"Bar1" => array(
"Baz1",
"Baz2"
)
),
"Foo2",
"Foo3" => array(
"Bar2",
"Bar3"
)
);
然后它显示我的错误,所以我把它改成了这个。它没有向我显示错误,但它没有在数据库中插入任何内容。有人能告诉我如何解决这个问题?感谢。
答案 0 :(得分:1)
简短的回答是:
您的代码正在插入来自isset()
的布尔结果值,这当然不是您的意图。删除isset()
调用并声明提交的值。
HOWEVER ,您的代码有很多问题需要解决。
对于有这么多问题的代码块,我很慷慨:
<?php
if (isset($_POST['submit']) {
if (empty($_POST['username']) || empty($_POST['email']) || empty($_POST['password']) || empty($_POST['passwordconf'])) {
$error = "<p class='errormsg'>Please fill out all the fields!</p>";
} elseif ($_POST['password'] !== $_POST['passwordconf']) {
$error = "<p class='errormsg'>You passwords do not match!</p>";
} elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
$error = "<p class='errormsg'>Email is not in name@domain format!</p>";
} else {
require 'connect.php'; // implement object-oriented $con variable
if ($stmt=$con->prepare("SELECT * FROM users WHERE username=?")) {
if ($stmt->bind_param('s', $_POST['username']) && $stmt->execute() && $stmt->store_result()) {
if ($stmt->num_rows) {
$error = "<p class='errormsg'>Username already exists</p>";
}
} else {
$error = "<p class='errormsg'>Username Check Statement Error</p>"; // $stmt->error
}
$stmt->close();
} else {
$error = "<p class='errormsg'>Username Check Prepare Error</p>"; // $con->error;
}
if ($stmt=$con->prepare("SELECT * FROM users WHERE email=?")) {
if ($stmt->bind_param('s', $_POST['email']) && $stmt->execute() && $stmt->store_result()) {
if ($stmt->num_rows) {
$error = "<p class='errormsg'>Email already exists</p>";
}
} else {
$error = "<p class='errormsg'>Email Check Statement Error</p>"; // $stmt->error
}
$stmt->close();
} else {
$error = "<p class='errormsg'>Email Check Prepare Error</p>"; // $con->error;
}
}
if ($error) {
echo $error;
} else {
// Perform your insert with $_POST['username'], $_POST['email'], $_POST['password'] , but DO NOT EVER, EVER, EVER store raw passwords...
// This subject is too extensive and gathers too much scrutiny for me to dare to post any hard-fast lines of code on StackOverflow
// Every minute that you spend researching this topic is time well spent.
// Not learning about cryptography and password security will lead to many, many unfortunate events for you and your users.
}
}
?>
所以,一般来说: