2015-10-17 124 views
0

我想做一个android登录系统,当我尝试注册时,我得到的是用户未能存储,我无法找到错误。我正在使用000webhost,我不知道它是否与此有关。请帮助和谢谢注册android系统错误php

我有Internet权限,并且登录正在工作,所以我认为不是连接或数据库的问题。当我尝试注册时,代码将转到“用户未能存储”部分,打印出“注册时发生未知错误!”。

这是register.php:

<?php 
    require_once 'include/DB_Functions.php'; 

$db = new DB_Functions(); 
// json response array 
$response = array("error" => FALSE); 

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) { 

    // receiving the post params 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

    // check if user is already existed with the same email 
    if ($db->isUserExisted($email)) { 
     // user already existed 
     $response["error"] = TRUE; 
     $response["error_msg"] = "User already existed with " . $email; 
     echo json_encode($response); 
    } else { 
     // create a new user 
     $user = $db->storeUser($name, $email, $password); 
     if ($user) { 
      // user stored successfully 
      $response["error"] = FALSE; 
      $response["uid"] = $user["unique_id"]; 
      $response["user"]["name"] = $user["name"]; 
      $response["user"]["email"] = $user["email"]; 
      $response["user"]["created_at"] = $user["created_at"]; 
      $response["user"]["updated_at"] = $user["updated_at"]; 
      echo json_encode($response); 
     } else { 
      // user failed to store 
      $response["error"] = TRUE; 
      $response["error_msg"] = "Unknown error occurred in registration!"; 
      echo json_encode($response); 
     } 
    } 
} else { 
    $response["error"] = TRUE; 

    $response["error_msg"] = "Required parameters (name, email or password) is missing!"; 
    echo json_encode($response); 
} 
?> 

这是DB_Functions.php类:

<?php 

class DB_Functions { 

    private $conn; 

    // constructor 
    function __construct() { 
     require_once 'DB_Connect.php'; 
     // connecting to database 
     $db = new Db_Connect(); 
     $this->conn = $db->connect(); 
    } 

    // destructor 
    function __destruct() { 

    } 

    public function consigueResultado($stmt) { 
     $RESULT = array(); 
     for ($i = 0; $i < $stmt->num_rows; $i++) { 
      $Metadata = $stmt->result_metadata(); 
      $PARAMS = array(); 
      while ($Field = $Metadata->fetch_field()) { 
       $PARAMS[] = &$RESULT[ $i ][ $Field->name ]; 
      } 
      call_user_func_array(array($stmt, 'bind_result'), $PARAMS); 
      $stmt->fetch(); 
     } 
     return $RESULT; 
    } 

    /** 
    * Storing new user 
    * returns user details 
    */ 
    public function storeUser($name, $email, $password) { 
     $uuid = uniqid('', true); 
     $hash = $this->hashSSHA($password); 
     $encrypted_password = $hash["encrypted"]; // encrypted password 
     $salt = $hash["salt"]; // salt 
     $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(:uuid, :name, :email, :encrypted_password, :salt, NOW())"); 
     $stmt->bindParam(':uuid', $uuid); 
     $stmt->bindParam(':name', $name); 
     $stmt->bindParam(':email', $email); 
     $stmt->bindParam(':encrypted_password', $encrypted_password); 
     $stmt->bindParam(':salt', $salt); 
     $result = $stmt->execute(); 
     //$this->conn = NULL; 
     //$stmt = NULL; 

     // check for successful store 
     if ($result) { 
      $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = :email"); 
      $stmt->bindParam(':email', $email); 
      $stmt->execute(); 
      $user = $this->consigueResultado($stmt); 
      //$this->conn = NULL; 
      $stmt = NULL; 

      return $user; 
     } else { 
      return false; 
     } 
    } 

    /** 
    * Get user by email and password 
    */ 
    public function getUserByEmailAndPassword($email, $password) { 

     $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = :email"); 

     $stmt->bindParam(':email', $email); 

     if ($stmt->execute()) { 
      $user = $this->consigueResultado($stmt); 
      //$this->conn = NULL; 
      $stmt = NULL; 

      return $user; 
     } else { 
      return NULL; 
     } 
    } 

    /** 
    * Check user is existed or not 
    */ 
    public function isUserExisted($email) { 
     $stmt = $this->conn->prepare("SELECT email from users WHERE email = :email"); 

     $stmt->bindParam(':email', $email); 

     $stmt->execute(); 

     if ($stmt->num_rows > 0) { 
      // user existed 
      //$this->conn = NULL; 
      $stmt = NULL; 

      return true; 
     } else { 
      // user not existed 
      //$this->conn = NULL; 
      $stmt = NULL; 
      return false; 
     } 
    } 

    /** 
    * Encrypting password 
    * @param password 
    * returns salt and encrypted password 
    */ 
    public function hashSSHA($password) { 

     $salt = sha1(rand()); 
     $salt = substr($salt, 0, 10); 
     $encrypted = base64_encode(sha1($password . $salt, true) . $salt); 
     $hash = array("salt" => $salt, "encrypted" => $encrypted); 
     return $hash; 
    } 

    /** 
    * Decrypting password 
    * @param salt, password 
    * returns hash string 
    */ 
    public function checkhashSSHA($salt, $password) { 

     $hash = base64_encode(sha1($password . $salt, true) . $salt); 

     return $hash; 
    } 

} 

?> 
+1

你必须更具体一些,当你尝试使用浏览器发布会发生什么?那样有用吗?使用邮递员(铬/铬应用程序)发送数据到该网址,如果有效,你在Android设备上得到的错误是什么?你确定它正在提出要求吗?你有'''android.permission.INTERNET'''权限吗? –

+0

我有Internet权限,并且登录正在工作,所以我认为这不是连接或数据库的问题。当我尝试注册时,代码将转到“用户未能存储”部分,打印出“注册时发生未知错误!”。 – PhoenixQoH

+0

你可以进入'''storeUser'''函数并尝试调试吗?检查程序流是否进入'''if($ result)'''block检查执行时实际采用哪条路径。 –

回答

0

好吧,我得到它的,而不是使用PDO使用过时的版本工作MySQL的。代码和数据库是正确的(我认为),我相信它给了我错误,因为000webhost不支持PDO或mysqli库。