2016-06-24 42 views
0

需要幫助這個PHP文件。我想發佈電子郵件,密碼,諾姆,endereco和telefone。後沒有得到Json的答覆後

首先驗證數據庫是否存在電子郵件和密碼(如果不插入)。

我不明白爲什麼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; 
    } 
} 
?> 
+0

最簡單的事情就是確保在if和else中獲得'var_dumb($ json)'的位置。看看你在哪裏,你有什麼。另外,我希望那些是清潔的投入。由於您正在使用msqli_ *查看參數綁定。 – nerdlyist

回答

0

也許您可以嘗試定義或分配給它之前的AA初始化$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)); 
     }