我试图用ajax进行表单验证。 我的问题是elseif语句无法正常工作
if (isset($_POST['submit'])){
//Create safe values for input into database
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
$company = mysqli_real_escape_string($mysqli, $_POST['company']);
$mail = mysqli_real_escape_string($mysqli, $_POST['mail']);
$adress = mysqli_real_escape_string($mysqli, $_POST['adress']);
$errorEmpty = false;
$errorTaken = false;
$errorMail = false;
//Check are required fields empty
if(empty($username) || empty($password) || empty($company) || empty($mail) || empty($adress)){
echo "<span class='form-error'>Fill in all fields!</span>";
$errorEmpty = true;
}
//Check is username already taken HERE IS THE PROBLEM!
elseif(isset($_POST['username'])){
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
if(!empty($username)){
$username_query = mysqli_query($mysqli, "SELECT * FROM users WHERE user_username = '$username'");
$count=mysqli_num_rows($username_query);
if($count!=0)
{
echo"<span class='form-error'>Username is already taken! </span>";
$errorTaken = true;
}
}
}
//Check is mail adress correct
elseif(!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'> Write a valid e-mail adress!</span>";
$errorMail = true;
}
else{
//Create user
$add_user_sql = "INSERT INTO users (user_username, user_password, user_company, user_mail, user_adress)
VALUES ('".$username ."', '".$password."', '".$company."', '".$mail."', '".$adress."')";
$add_user_res = mysqli_query($mysqli, $add_user_sql)
or die(mysqli_error($mysqli));
//Close connection to MySQL
mysqli_close($mysqli);
echo "<span class='form-success'>Succesifuly registrated!</span>";
}
}
问题是当我检查已经在数据库中的用户名时,因为在该步骤之后,每个下一个elseif语句都不会执行。当我把它放在第一位时它停止执行其余的代码,同样的情况是当我把它放在最后的位置。其他检查空字段和邮件验证的条件正常工作。
答案 0 :(得分:1)
创造&#34;问题&#34;检查您的$ _POST [&#39;用户名&#39;]。
elseif(isset($_POST['username'])){
如果设置了$ _POST [&#39;用户名&#39;],则if-else语句不会更深入(这是如何工作的)。 我会建议这样的事情来解决你的问题:
// check if it was sent through your form
if (isset($_POST['submit']))
{
//Create safe values for input into database
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
$company = mysqli_real_escape_string($mysqli, $_POST['company']);
$mail = mysqli_real_escape_string($mysqli, $_POST['mail']);
$adress = mysqli_real_escape_string($mysqli, $_POST['adress']);
$errorEmpty = false;
$errorTaken = false;
$errorMail = false;
// check if any of the fields are empty
if(empty($username) || empty($password) || empty($company) || empty($mail) || empty($adress))
{
echo "<span class='form-error'>Fill in all fields!</span>";
$errorEmpty = true;
}
//if none of the fields are empty - check if username exists
$username_query = mysqli_query($mysqli, "SELECT * FROM users WHERE user_username = '$username'");
$count=mysqli_num_rows($username_query);
if($count!=0)
{
echo"<span class='form-error'>Username is already taken! </span>";
$errorTaken = true;
}
//Check is mail adress correct
if(!filter_var($mail, FILTER_VALIDATE_EMAIL))
{
echo "<span class='form-error'> Write a valid e-mail adress!</span>";
$errorMail = true;
}
// check if any of the errors are set to true
if(!$errorEmpty AND !$errorMail AND !$errorTaken)
{
//Create user
$add_user_sql = "INSERT INTO users (user_username, user_password, user_company, user_mail, user_adress)
VALUES ('".$username ."', '".$password."', '".$company."', '".$mail."', '".$adress."')";
$add_user_res = mysqli_query($mysqli, $add_user_sql) or die(mysqli_error($mysqli));
//Close connection to MySQL
mysqli_close($mysqli);
echo "<span class='form-success'>Succesifuly registrated!</span>";
}
}
答案 1 :(得分:0)
尝试从命令行运行脚本,或在脚本顶部添加此行:
ini_set('display_errors', 1);
通过这种方式,您可以看到哪个错误正在停止执行。
答案 2 :(得分:0)
通过编写此代码解决了问题,无论如何,感谢大家愿意提供帮助
if (isset($_POST['submit'])){
//Create safe values for input into database
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
$company = mysqli_real_escape_string($mysqli, $_POST['company']);
$mail = mysqli_real_escape_string($mysqli, $_POST['mail']);
$adress = mysqli_real_escape_string($mysqli, $_POST['adress']);
$errorEmpty = false;
$errorTaken = false;
$errorMail = false;
//Check are required fields empty
if(empty($username) || empty($password) || empty($company) || empty($mail) || empty($adress)){
echo "<span class='form-error'>Fill in all fields!</span>";
$errorEmpty = true;
}
//Check is mail adress correct
elseif(!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'> Write a valid e-mail adress!</span>";
$errorMail = true;
}
//Check is username already taken
if(isset($_POST['username'])){
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
if(!empty($username)){
$username_query = mysqli_query($mysqli, "SELECT * FROM users WHERE user_username = '$username'");
$count=mysqli_num_rows($username_query);
if($count!=0)
{
echo"<span class='form-error'>Username is already taken! </span>";
$errorTaken = true;
}
}
}
if($errorEmpty == false && $errorMail == false && $errorTaken == false){
//Create user
$add_user_sql = "INSERT INTO users (user_username, user_password, user_company, user_mail, user_adress)
VALUES ('".$username ."', '".$password."', '".$company."', '".$mail."', '".$adress."')";
$add_user_res = mysqli_query($mysqli, $add_user_sql)
or die(mysqli_error($mysqli));
//Close connection to MySQL
mysqli_close($mysqli);
echo "<span class='form-success'>Succesifuly registrated!</span>";
}
}