我有以下代碼:
if($name !=null && $email !=null && $password !=null && $gender !=null) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = "you should write a correct email";
}
if(strlen($password) < 6) {
$passwordError = "password should consist from 6 charachters or more";
}
$result = mysqli_query($conn, $sql);
if($result) {
header("location:index.php");
}else {
echo "Error";
}
} else {
$msg = "you should fill all the form";
}
}
當郵箱不正確或密碼小于6時它不會顯示任何錯誤,并且可以正常插入資料,如果密碼和郵箱都不正確,我需要顯示例如兩條錯誤訊息,我該怎么做?
先謝謝了
uj5u.com熱心網友回復:
修改您的代碼,以便
- 如果有錯誤,它將不會執行資料庫查詢
- 顯示錯誤(如果存在)。
<?php
//initialize the error messages to be blank
$emailError ="";
$passwordError="";
if($name !=null && $email !=null && $password !=null && $gender !=null) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = " you should enter a correct email ;";
}
if(strlen($password) < 6) {
$passwordError = " password should consist of 6 characters or more ;";
}
if ($emailError =="" && $passwordError=="") {
$result = mysqli_query($conn, $sql);
if($result) {
header("location:index.php");
} else {
echo "Error";
}
} else {
echo "You have the following error(s) :";
if ($emailError !=""){
echo $emailError;
}
if ($passwordError !=""){
echo $passwordError;
}
}
} else {
// $msg = "you should fill all the form";
echo "you should enter all the required data in the form";
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524334.html
標籤:phphtml验证
