如何解决同时显示2条消息的错误消息

时间:2019-05-21 09:44:48

标签: php html

当用户名已经存在于数据库中并且用户输入了使用的邀请代码时,它会显示2条错误消息,并且弄乱了我的注册表格。

如何使错误一次显示一次? 例如,如果用户输入的用户名已经在数据库中,并且输入了使用的邀请代码,它将显示“代码已使用”

表单功能

function register(){
    global $db, $errors, $username;

    $username    =  mysqli_real_escape_string($db, $_POST['username']);
    $password  =  mysqli_real_escape_string($db, $_POST['password']);
    $inviteCode  =  mysqli_real_escape_string($db, $_POST['inviteCode']);


  $user_check_query = "SELECT * FROM users WHERE username='$username' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) {
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }
  }

/////////////////////////////////////////////
$rs_check = mysqli_query($db, "SELECT `taken` from ic WHERE inviteCode='$inviteCode'"); 
if (0 == mysqli_num_rows($rs_check)) { 
array_push($errors, "Invalid Code");
}
else{
$token = mysqli_fetch_assoc($rs_check);
        if ($token['taken'] > 0) {
            array_push($errors, "Code is already used");
        } elseif (mysqli_query($db, "UPDATE ic SET taken='1' WHERE inviteCode='$inviteCode'")){
        }
}
////////////////////////////////////////////

    if (count($errors) == 0) {
        $password = md5($password);

        if (isset($_POST['user_type'])) {
            $user_type = e($_POST['user_type']);
            $query = "INSERT INTO users (username, user_type, password) 
                      VALUES('$username', '$user_type', '$password')";
            mysqli_query($db, $query);

        }else{
            $query = "INSERT INTO users (username, user_type, password) 
                      VALUES('$username', 'user', '$password')";
            mysqli_query($db, $query);

            $logged_in_user_id = mysqli_insert_id($db);

            $_SESSION['user'] = getUserById($logged_in_user_id);
            $_SESSION['success']  = "You are now logged in";
            header('location: index.php');              
        }
    }
}

错误消息的功能

function display_error() {
    global $errors;

    if (count($errors) > 0){
        echo '<div class="alert alert-danger alert-dismissible show fade">
                      <div class="alert-body">
                        <button class="close" data-dismiss="alert">
                          <span>&times;</span>
                        </button><b><center>';
            foreach ($errors as $error){
                echo $error .'</b></center></div></div>';
            }
    }
}   

我如何在html中显示错误消息

<p><?php echo display_error(); ?></p>

1 个答案:

答案 0 :(得分:0)

我个人建议显示与用户输入有关的所有错误消息。

  1. 合并错误文本
  2. 显示多个更改危险框。

1。合并错误文本

只需合并错误,而不关闭foreach中的标签即可。

function display_error() {
global $errors;

    if (count($errors) > 0){
        echo '<div class="alert alert-danger alert-dismissible show fade">
                      <div class="alert-body">
                        <button class="close" data-dismiss="alert">
                          <span>&times;</span>
                        </button><b><center>';
            foreach ($errors as $error){
                echo $error .'<br />';
            }
            echo '</b></center></div></div>';
    }
}

2。显示多个备用危险框

function display_error() {
    global $errors;

    if (count($errors) > 0){
            foreach ($errors as $error){
                echo '<div class="alert alert-danger alert-dismissible show fade">
                      <div class="alert-body">
                        <button class="close" data-dismiss="alert">
                          <span>&times;</span>
                        </button><b><center>' . $error .'</b></center></div></div>';
            }
    }
}   

如果您确实只想显示一个错误,则可以执行以下操作:

function display_error() {
    global $errors;

    if (count($errors) > 0){
                echo '<div class="alert alert-danger alert-dismissible show fade">
                      <div class="alert-body">
                        <button class="close" data-dismiss="alert">
                          <span>&times;</span>
                        </button><b><center>' . $errors[0] .'</b></center></div></div>';
    }
}