PHP - 当语句为真时隐藏元素

PHP - Hide elements when the statement is true

我有这个代码来隐藏元素,但第一部分是隐藏的,第二部分总是显示的,无论我是以用户还是管理员身份登录都没有关系。我不明白那里出了什么问题...

我希望当我以用户身份登录时显示第一个下拉内容,当我以管理员身份登录时显示第二个下拉内容,但是当我以用户或管理员身份登录时只显示第二个下拉内容。

代码:

<?php
if($parola == $data['parola'] && $data['admin'] == '0'){?>
<div class="dropdown">
  <button class="dropbtn"><?php echo $_SESSION['mail']; ?></button>
  <div class="dropdown-content">
    <a href="adauga.php">Adauga anunt</a>
    <a href="anunturi.php">Anunturile mele</a>
    <a href="edit.php">Editeaza profil</a>
 <a href="logout.php">Delogeaza-te</a>
  </div>
</div> 
<?php } else { ?>  
<div class="dropdown">
  <button class="dropbtn"><?php echo $_SESSION['mail']; ?></button>
  <div class="dropdown-content">
    <a href="/admin/index.php">Control Panel</a>
 <a href="logout.php">Delogeaza-te</a>
  </div>
</div> 
<?php } ?>

用户、管理员和禁止用户的登录。

<?php
 require 'config.php';
 if(isset($_POST['login'])) {
  $errMsg = '';
  // Get data from FORM
  $mail = $_POST['mail'];
  $parola = $_POST['parola'];
  if($mail == '')
   $errMsg = 'Introduceti adresa de mail.';
  if($parola == '')
   $errMsg = 'Introduceti parola.';
  if($errMsg == '') {
   try {
    $stmt = $connect->prepare('SELECT mail, parola, admin, banned FROM acc_table WHERE mail = :mail');  
    $stmt->execute(array(
     ':mail' => $mail
     ));
    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    if($data == false){
     $errMsg = "Contul asociat cu mail-ul $mail nu a fost gasit.";
    }
    else {
     if($parola == $data['parola'] and $data['admin'] == '1') {
      $_SESSION['mail'] = $data['mail'];
      $_SESSION['parola'] = $data['parola'];
      $_SESSION['admin'] = $data['admin'];
      header('Location: ../admin/index.php');
      exit;
     }
     else
      $errMsg = 'Parola nu este buna.';
    }
    
     if($parola == $data['parola'] and $data['banned'] == '1') {
      $_SESSION['mail'] = $data['mail'];
      $_SESSION['parola'] = $data['parola'];
      $_SESSION['banned'] = $data['banned'];
      header('Location: ban.html');
      exit;
     }
     else
      $errMsg = 'Parola nu este buna.';
     
     if($parola == $data['parola'] and $data['admin'] == '0') {
      $_SESSION['mail'] = $data['mail'];
      $_SESSION['parola'] = $data['parola'];
      $_SESSION['admin'] = $data['admin'];
      header('Location: profile.php');
      exit;
     }
     else
      $errMsg = 'Parola nu este buna.';
         
   }
   catch(PDOException $e) {
    $errMsg = $e->getMessage();
   }
  }
 }

试试下面的代码看看是否适合你。

如果这不起作用,请告诉我您遇到了什么错误。

<?php
require 'config.php';

if (isset($_POST['login'])) {

    $errMsg = '';
    // Get data from FORM
    $mail = $_POST['mail'];
    $parola = $_POST['parola'];
    if ($mail == '') {
        $errMsg = 'Introduceti adresa de mail.';
    } else if ($parola == '') {
        $errMsg = 'Introduceti parola.';
    } else {
        if ($errMsg == '') {
            try {
                $stmt = $connect->prepare("SELECT mail, parola, admin, banned FROM acc_table WHERE mail = :mail");
                $stmt->bindParam(':mail', $var_mail);
                $var_mail = $_POST['mail'];
                $stmt->execute();
                if ($data = $stmt->fetch()) {
                    if ($parola == $data['parola'] and $data['admin'] == '0') {
                        $_SESSION['mail'] = $data['mail'];
                        $_SESSION['parola'] = $data['parola'];
                        $_SESSION['admin'] = $data['admin'];
                        header('Location: profile.php');
                        exit;
                    } else if ($parola == $data['parola'] && $data['admin'] == '1') {
                        $_SESSION['mail'] = $data['mail'];
                        $_SESSION['parola'] = $data['parola'];
                        $_SESSION['admin'] = $data['admin'];
                        header('Location: ../admin/index.php');
                        exit;
                    } else if ($parola == $data['parola'] && $data['banned'] == '1') {
                        $_SESSION['mail'] = $data['mail'];
                        $_SESSION['parola'] = $data['parola'];
                        $_SESSION['banned'] = $data['banned'];
                        header('Location: ban.html');
                        exit;
                    } else {
                        $errMsg = 'Parola nu este buna.';
                    }
                } else {
                    $errMsg = "Contul asociat cu mail-ul ".$var_mail." nu a fost gasit.";
                }
            }
            catch(PDOException $e) {
                $errMsg = $e->getMessage();
            }
        }
    }
}

像这样放置 if 条件 :-

if($data['admin'] === '0'){
    //The rest code runs here
}

让我知道这是否有效。

我找到了解决问题的方法。

他没有使用 header 重定向,而是使用 JavaScript。 我用强制用户留在页面上:

<?php
 if(['admin'] == '1')?>
  <script>
  window.location = 'admin/index.php';
  </script>

感谢大家帮助我解决这个问题。