Hello Guy的请求协助,我有验证问题。所以基本上如果我不验证任何形式的表单执行完美,那么一旦我验证它,表单只是返回表单及其值。
**这是我的代码**
$error[] = "";
if (isset($_POST['submit'])) {
$firstname = trim($_POST['firstname']);
$lastname = trim($_POST['lastname']);
$user_name = trim($_POST['user_name']);
$user_type = trim($_POST['user_type']);
$email = trim($_POST['email']);
$created_at = trim($_POST['created_at']);
$password = trim($_POST['password']);
$confirm_password = trim($_POST['confirm_password']);
// validate form field
if (empty($firstname)) {
$error[] = 'Field empty, please enter your first name';
} else {
if (strlen($firstname) < 3) {
$error[] = 'First Name is too short';
}
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/", $firstname)) {
$error[] = "Only letters and white space allowed";
}
if (empty($lastname)) {
$error[] = 'Field empty, please enter your last name';
} else {
if (strlen($lastname) < 3) {
$error[] = 'Last Name is too short';
}
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/", $lastname)) {
$error[] = "Only letters and white space allowed";
}
if (empty($user_name)) {
$error[] = 'Field empty, please enter your username';
} else {
if (strlen($user_name) < 3) {
$error[] = 'UserName is too short';
}
}
// set email filter validation
if (empty($email)) {
$error[] = 'Field empty, please enter your email address';
} else {
//email validation
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error[] = 'Please enter a valid email address';
}
}
if (empty($password)) {
$error[] = 'Field empty, please create a password';
} else {
if (strlen($password) < 6) {
$error[] = 'Password is too short';
}
if (strlen($password) > 15) {
$error[] = 'Password is too long';
}
if (!preg_match("#[A-Z]+#", $password)) {
$error[] = "Password must include at least one CAPS! ";
} else {
if (!preg_match("#[0-9]+#", $password)) {
$error[] = "Password must include at least one NUMBER! ";
}
}
}
// set field validation for confirm password
if (empty($confirm_password)) {
$error[] = 'Field empty, please confirm your password';
} else {
if ($password != $confirm_password) {
$error[] = 'Error... Passwords do not match';
}
}
//if no errors have been created carry on
if (!isset($error)) {
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$created_at = date('Y-m-d');
if (!($stmt = $con - > prepare("INSERT INTO user (firstname, lastname, user_name, user_type, email, password, created_at)
VALUES( ? , ? , ? , ? , ? , ? , ? )
"))) {
echo "Prepare failed: (".$con - > errno.
")".$con - > error;
}
if (!$stmt - > bind_param('sssssss', $firstname, $lastname, $user_name, $user_type, $email, $password_hash, $created_at)) {
echo "Binding paramaters failed:(".$stmt - > errno.
")".$stmt - > error;
}
if (!$stmt - > execute()) {
echo "Execute failed: (".$stmt - > errno.
")".$stmt - > error;
}
$stmt - > close();
if ($stmt) {
$_SESSION['main_notice'] = "Successfully registered, login here!";
header('Location: index.php');
exit;
} else {
$_SESSION['main_notice'] = "Some error, try again";
header('Location: '.$_SERVER['PHP_SELF']);
}
}
}
如果你能看到我不能看到的东西,我不会不。提前谢谢。
答案 0 :(得分:2)
isset
检查variantbe 是否已设置,即it has a value
您的if(!isset($error)){
永远不会真实。因为您在脚本中先前为$error
设置了值:
$error[] = "";
此行错误。您向$error
添加一个空字符串。为什么?只需将其声明为空数组:
$error = [];
之后,您可以检查$error
empty()
而不是isset
的空虚:
//if no errors have been created carry on
if(empty($error)) {
答案 1 :(得分:0)
我建议将其略有不同。
首先检测表单已发布时执行检查。如果在输出任何内容之前执行此操作,您仍然可以发送标题并执行其他有用的操作。
$sError = "";
$sFirstName = '';
// check if post has been made
if(isset($_POST['submit'])){
//check for content and a non empty string
if(isset($_POST['FirstName']) && $_POST['FirstName'] != ''){
// perform any other validation tasks here
if(strlen($_POST['FirstName'] > 3){
$sFirstName = $_POST['FirstName'];
}else{
$sError .= "[FirstNameLength]";
}
}
// if firstname hasnt been set we can add to error string
if($sFirstName == '')
$sError .= "[FirstName]";
// if nothing in sError go ahead with the action
if($sError = ""){
}
然后在您包含标题等之后,您可以检查sError var并输出任何错误消息;
if($sError !== ""){
if(
strpos($sError, '[FirstName]') !== false ||
strpos($sError, '[LastName]') !== false
){
echo"<div class=\"statusMessage alert alert-danger\" role=\"alert\"><ul>";
if(strpos($sError, '[FirstName]') !== false){
echo "<li>You must enter a first name</li>";
}
if(strpos($sError, '[FirstNameLength]') !== false){
// just an example of another check, firstname could be 1 char!
echo "<li>Your first name is too short, please enter full name</li>";
}
echo "</ul></div>";
}
}
// success messages
if(isset($_GET['successMessage'])){
if($_GET['successMessage'] === "info-updated"){
echo"<div>Info Updated.</div>";
}
}