解析错误:语法错误,第 95 行 C:\xampp\htdocs\PMSS\login.php 中的文件意外结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20549250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:16:44  来源:igfitidea点击:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\PMSS\login.php on line 95

php

提问by Nick

This is my Login.php

这是我的 Login.php

<?php

//load and connect to MySQL database stuff
require("config.inc.php");

if (!empty($_POST)) {

    if(empty($_POST['username']) || empty($_POST['password'])) {

        $response["success"] = 0;
        $response["message"] = "Please fill in the login details!";
        die(json_encode($response));
    }

    $query = "SELECT  email, password, position FROM user   WHERE   email = :email ";

    $query_params = array(':email' => $_POST['username'],);

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage());

    //or just use this use this one to product JSON data:
        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die(json_encode($response));

    }

    //This will be the variable to determine whether or not the user's information is correct.
    //we initialize it as false.
    $validated_info = false;
    $login_ok = false;

    //fetching all the rows from the query
    $row = $stmt->fetch();
    if ($row) {
        //if we encrypted the password, we would unencrypt it here, but in our case we just
        //compare the two passwords
        if ($_POST['password'] === $row['password']) {
            $login_ok = true;       
        }

    // If the user logged in successfully, then we send them to the private members-only page 
    // Otherwise, we display a login failed message and show the login form again 
        if ($login_ok) {
            $response["success"] = 1;
            $response["message"] = "Login Successful!";
            $response["posts"]   = array();

                foreach ($row as $rerow) {
                $row = array(
                $post["position"] = $rerow["position"]
                            );

                array_push($response["posts"], $post);
                }

                die(json_encode($response));
        } 
        else {
            $response["success"] = 0;
            $response["message"] = "Invalid Credentials!";
            die(json_encode($response));
        }

    } 
}   
else {
    ?>
    <h1>Login</h1> 
    <form action="login.php" method="post"> 
        Username:<br /> 
        <input type="text" name="username" placeholder="username" /> 
        <br /><br /> 
        Password:<br /> 
        <input type="password" name="password" placeholder="password" value="" /> 
        <br /><br /> 
        <input type="submit" value="Login" /> 
    </form> 
    <a href="register.php">Register</a>
    <?php
}
?> 

I not sure which place I didn't put close bracket. Can you all help me check out see? I not sure where to put the close bracket. Is is anything to do with my condition while retrieving my data to my android? If it is, please state it out as for me to know the syntax I write was correct or not.

我不确定哪个地方我没有放右括号。大家可以帮我看看吗?我不知道把右括号放在哪里。在将我的数据检索到我的 android 时,这与我的状况有什么关系吗?如果是,请说出来让我知道我写的语法是否正确。

回答by rray

is missing the }for if (!empty($_POST)) {

缺少}用于if (!empty($_POST)) {

    if (!empty($_POST)) {


     //code...

    } else {
    ?>
      <h1>Login</h1>
      <form action="login.php" method="post">
          Username:<br />
          <input type="text" name="username" placeholder="username" />
          <br /><br />
           Password:<br />
           <input type="password" name="password" placeholder="password" value="" />
           <br /><br />
           <input type="submit" value="Login" />
       </form>
      <a href="register.php">Register</a>
    <?php
    }
} // <---- this is missing

回答by Kevin Cittadini

If I may, I'd like to tell you that your code is kinda messy. Indentation saves your life, remmeber.

如果可以,我想告诉你,你的代码有点乱。缩进可以挽救您的生命,请记住。



Anyway I've seen that no errors is found if you remove the fifthline of your code:

无论如何,我已经看到如果删除代码的第五行,则不会发现任何错误:

if (!empty($_POST)) {

This is not closed. if ($row) {is closed just before the elsethat is above the login heading title. As matter of fact, if the DB doesn't return any result, it shows the login page.

这不是关闭的。在登录标题标题上方的if ($row) {之前关闭else。事实上,如果数据库没有返回任何结果,它会显示登录页面。

Anyway the fifthline is unecessary since just below it you do the control on specific vars. You don't have to check the full $_POSTarray.

无论如何,第五行是不必要的,因为在它下面你可以控制特定的变量。您不必检查完整$_POST数组。