警告:非法的字符串偏移量';位置';在C:examplephtdocsPMSSlogin.p


Warning: Illegal string offset 'position' in C:xampphtdocsPMSSlogin.php on line 57

这是我的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
}
?> 

警告出现三次,同时显示

{"成功":1,"消息":"登录成功

我正确输入了我的登录id和密码,我想从我的数据库中检索值为"user"的位置,为什么它只显示j 1 u

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

应该是

            $row = array(
            $post["position"] => $rerow["position"]
                        );

事实上

        foreach ($row as $rerow) {
            $row = array(
                $post["position"] = $rerow["position"]
            );
            array_push($response["posts"], $post);
        }

也许你想做其他事情:

do {
    $post = $row["position"]
    array_push($response["posts"], $post);
} while ($row = $stmt->fetch());

这会将字段"position"添加到posts中,并继续获取其他记录。

相关文章:
  • 没有找到相关文章