【问题标题】:Unexpected '<' error in post by POSTMAN chrome appPOSTMAN chrome 应用程序在帖子中出现意外的“<”错误
【发布时间】:2016-07-02 17:07:39
【问题描述】:

//config.php 文件

    <?php
    define('hostname', 'localhost');
    define('username', 'root');
    define('password', '');
    define('db_name', 'tutorial2');
    ?>

//connection.php文件

 <?php
    require_once 'config.php';
    class DB_Connection {
    private $connect;
    function _construct(){
        $this->connect = mysqli_connect(hostname, username, password, db_name) or die(" DB connection error");

    }
    public function get_connection() {
        return $this->connect;
    } 
    }
    ?>

//user_control.php 文件。我想在我的 phpMyadmin-mysql 数据库中插入“电子邮件”和密码地址。

 <?php
    require_once 'connection.php';
    header('Content-Type: application/json ');
    class User {
    private $db;
    private $connection;

    function __construct(){
        $this->db = new DB_Connection();
        $this->connection = $this->db->get_connection();
    }
    public function does_user_exist($email,$password)
    {
        $query = "Select * from users where email = '$email' and password = '$password'";
        $result = mysqli_query($this->connection, $query);
        if (mysqli_num_rows($result) > 0){
            $json[success] = ' Welcome '.$email;
            echo json_encode($json);
            mysqli_close($this->connection);
        }else {
            $query = " Insert into users(email,password) values ('$email','$password')";
            $is_inserted = mysqli_query($this->connection, $query);
            if ($is_inserted == 1){
                $json[success] = ' Account created, welcome '.$email;
            }else {
                $json[error] = ' Wrong password ';
            }
            echo json_encode($json);
            mysqli_close($this->connection);
        }
    }
    }

    $user = new User();
    if (isset($_POST['email'],$_POST['password'])){
    $email = $_POST['email'];
    $password = $_POST['password'];

    if (!empty($email) && !empty($password)){

        $encrypted_password = md5($password);
        $user -> does_user_exist($email,$encrypted_password);

    }else {
        echo json_encode("You must fill both fields");
    }
    }
    ?>

毕竟POSTMAN POST error. somehow i did by cleaning but it dosent save the "email" and "password" into database.

【问题讨论】:

    标签: php json


    【解决方案1】:

    原因如下:

    if (!empty($email) && !empty($password)){
        $encrypted_password = md5($password);
        $user -> does_user_exist($email,$encrypted_password);
    

    删除空格: $user-&gt;does_user_exist($email,$encrypted_password);

    确保在所有文件上开始 &lt;?phptag 之前没有可用的空格。

    还要确保您的 $json 数组键在 ' (single quotes) 内,如下所示:

    $json['success'] = ' Welcome '.$email;
    $json['error'] = ' Wrong password ';
    

    【讨论】:

    • 感谢您的回复,但仍然是“意外'
    • 您能否确保在所有 PHP 文件的开头,&lt;?php 标记之前没有空格?
    • 先生输出仍然没有变化。 $json['成功'] = '欢迎'; $json['success'] = '账号创建,欢迎'; $json['error'] = '密码错误';
    • @MuhammadSumonMollaSelim,请问你是如何解决这个问题的
    • @SKMishu 请问你是怎么解决这个问题的?
    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2022-01-21
    • 2021-04-01
    • 2021-11-16
    相关资源
    最近更新 更多