Can PHP foreach() handle }else{ and }elseif{?


Can PHP foreach() handle }else{ and }elseif{?

我正在尝试使我的登录工作,问题是每当我按"注册"时,我都会收到错误,因为我看到第 112 行是 }else{,所以我想知道,是否有针对 ELSE 部分的解决方法?谢谢!

解析错误:

语法错误,F:''xampp''htdocs''SocialMedia''first''index.php 第 112 行中意外的"else"(T_ELSE

(

这是我的代码:

if(isset($_POST['user_login']) && isset($_POST['password_login'])){
  $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['user_login']);
  $password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password_login']);
  $password_login_md5 = md5($password_login);
  $sql = $databaseConnection->prepare('SELECT id,username,password, FROM  users WHERE username = :user_login, password = ":password_login_md5"');
  $sql->bindParam(':user_login', $user_login);
  $sql->bindParam(':password_login_md5', $password_login_md5);
  $sql->execute();
  $userCount = $sql->rowCount();
  foreach($userCount as $row){
    if($row > 0){
      $id = $row['id'];
    }
    $_SESSION["user_login"] = $user_login;
    header("Location: index.php");
    exit();
  }else{
    echo "That information is incorrect, try again";
  }
}
?>

提前感谢!

编辑:这就是我现在拥有的:

if(isset($_POST['user_login']) && isset($_POST['password_login'])){
  $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['user_login']);
  $password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password_login']);
  $password_login_md5 = md5($password_login);
  $sql = $databaseConnection->prepare('SELECT id,username,password, FROM  users WHERE username = :user_login, password = ":password_login_md5"');
  $sql->bindParam(':user_login', $user_login);
  $sql->bindParam(':password_login_md5', $password_login_md5);
  $sql->execute();
  $userCount = $sql->rowCount();
  if($userCount){
    foreach($userCount as $row){
      if($row > 0){
        $id = $row['id'];
      }
    }
  }else{
    echo "information incorrect";
  }
}

那里没有错误,只要我按登录,我就会收到此错误:

致命错误:未捕获的异常"PDOException",消息为"SQLSTATE[HY093]:参数编号无效:绑定变量数与 F:''xampp''htdocs''SocialMedia''first''index.php:101 中的令牌数不匹配 堆栈跟踪:#0 F:''xampp''htdocs''SocialMedia''first''index.php(101

(: PDOStatement->execute(( #1 {main} 在第 101 行的 F:''xampp''htdocs''SocialMedia''first''index.php 中抛出

我想,我 100% 确定这是我的查询......

你应该检查$userCount变量。没有foreach else.

if ($userCount) {
    foreach($userCount as $row){
        ...
    }
 }else{
    ...
 }