发布后没有得到Json的回答

时间:2016-06-24 14:19:15

标签: php json

需要这个php文件的帮助。我想发布电子邮件,密码,nome,endereco和telefone。

首先验证数据库中是否有电子邮件和密码,如果没有插入。

我不明白为什么Json没有回应。

<?php
include_once 'connection.php';

class User {

    private $db;
    private $connection;

    function __construct() {
        $this -> db = new DB_Connection();
        $this -> connection = $this->db->getConnection();
    }

    public function does_user_exist($email,$password, $nome, $endereco, $telefone)
    {
        $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, nome, endereco, telefone) values ( '$email','$password', '$nome', '$endereco', '$telefone')";
            $inserted = mysqli_query($this -> connection, $query);
            if($inserted == 1 ){
                $json['success'] = 'Acount created';
            }else{
                $json['error'] = 'Wrong password';
            }
            echo json_encode($json);
            mysqli_close($this->connection);
        }

    }

}


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


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

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

    }else{
        echo json_encode("you must type both inputs");
    }

}
?>

这是我的连接文件...还有配置数据库名称密码和用户....

<?php
require_once 'config.php';

class DB_Connection {

    private $connect;
    function __construct() {
        $this->connect = mysqli_connect(hostname, username, password, db_name)
        or die("Could not connect to db");

    }

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

1 个答案:

答案 0 :(得分:0)

也许您可能会先尝试定义或初始化$json,然后再为其赋值,尤其是因为它不是数字索引数组。以下是人们的建议:

    <?php
        include_once 'connection.php';

        class User {

            private $db;
            private $connection;

            function __construct() {
                $this -> db = new DB_Connection();
                $this -> connection = $this->db->getConnection();
            }

            public function does_user_exist($email,$password, $nome, $endereco, $telefone){
                $json       = array();  // EXPLICITLY INITIALIZE $json TO AN EMPTY ARRAY...
                $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;
                    mysqli_close($this -> connection);
                    die( json_encode($json) );
                }else{
                    $query      = "insert into USERS (email, password, nome, endereco, telefone) values ( '{$email}','{$password}', '$nome', '{$endereco}', '{$telefone}')";
                    $inserted   = mysqli_query($this -> connection, $query);

                    if($inserted == 1 ){
                        $json['success']    = 'AcCount created';
                    }else{
                        $json['error']      = 'Wrong password';
                    }
                    mysqli_close($this->connection);
                    die ( json_encode($json) );
                }

            }

        }


        $user       = new User();
        $email      = isset($_POST['email'])    ? htmlspecialchars(trim($_POST['email']))       : null;
        $password   = isset($_POST['password']) ? htmlspecialchars(trim($_POST['password']))    : null;
        $nome       = isset($_POST['nome'])     ? htmlspecialchars(trim($_POST['nome']))        : null;
        $endereco   = isset($_POST['endereco']) ? htmlspecialchars(trim($_POST['endereco']))    : null;
        $telefone   = isset($_POST['telefone']) ? htmlspecialchars(trim($_POST['telefone']))    : null;

        if(!empty($email) && !empty($password)){
            $encrypted_password = md5($password);
            $user-> does_user_exist($email,$password, $nome, $endereco, $telefone);
        }else{
            $arrMessage = array(
                "message"   => "you must type both inputs"
            );
            die( json_encode($arrMessage) );
        }