PHP解析错误:语法错误,意外出现“ {”

时间:2019-12-21 11:45:17

标签: php

我的代码中有Parse错误语法错误,意外的'{' 你有解决方案吗?我一直在寻找缺少的括号,但是却找不到办法,谢谢您的帮助

<?php 
require_once '../src/database.php';
if (isset($_POST)) {
  if (!empty(htmlspecialchars($_POST['username']) AND !empty(htmlspecialchars($_POST['password']))) {
    $req = $db->prepare('SELECT * FROM users WHERE username = ? AND password = ?');
    $req->execute([
      'username' => $_POST['username'],
      'password' => $_POST['password']


    ]);

      $user = $req->fetchObject();
      if ($user) {
      $_SESSION['admin'] = $_POST['username']; 
      header('location.index.php');
      }else{
        $error = 'identifiants incorrect';
      }
    }
  else{
    $error = 'Veuillez remplir tous les champs';
  }
}
if(isset($error)) {
  echo '<div class="error">'. $error .'</div>';
}

 ?>

1 个答案:

答案 0 :(得分:1)

您忘记为)关闭htmlspecialchars()

如果您使用的是某些编辑器,那么您会更好地知道错误在哪里。

if (isset($_POST)) {
  if ( !empty(htmlspecialchars($_POST['username'])) AND !empty(htmlspecialchars($_POST['password']))) {
    $req = $db->prepare('SELECT * FROM users WHERE username = ? AND password = ?');
    $req->execute([
      'username' => $_POST['username'],
      'password' => $_POST['password']


    ]);

      $user = $req->fetchObject();
      if ($user) {
      $_SESSION['admin'] = $_POST['username']; 
      header('location.index.php');
      }else{
        $error = 'identifiants incorrect';
      }
    }
  else{
    $error = 'Veuillez remplir tous les champs';
  }
}
if(isset($error)) {
  echo '<div class="error">'. $error .'</div>';
}