2015-07-20 107 views
-1

我想創建一個PHP腳本來註冊用戶。它將我的xCode與mySQL數據庫連接起來。註冊用戶PHP

我收到以下錯誤:

8ee52684907bd42381d94f74f3c4d321b17c5285 Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/SwiftAppAndMYSQL/db/MySQLDAO.php on line 76

Fatal error: Uncaught exception 'Exception' in /Applications/XAMPP/xamppfiles/htdocs/SwiftAppAndMYSQL/db/MySQLDAO.php:76 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/SwiftAppAndMYSQL/scripts/registerUser.php(63): MySQLDAO->registerUser('email', 'gui', 'Maia', '8ee52684907bd42...', '\x99\x99S'eXqs\xE0\xC4\x80[\xB1\x07y...') #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/SwiftAppAndMYSQL/db/MySQLDAO.php on line 76

這是我registerUser腳本

<?php 

require ("../db/MySQLDAO.php"); 
require ("../db/Conn.php"); 

$returnValue = array(); 

if (
     empty($_REQUEST["userEmail"]) || 
     empty($_REQUEST["userPassword"]) || 
     empty($_REQUEST["userFirstName"]) || 
     empty($_REQUEST["userLastName"])) { 

    $returnValue["status"] = "400"; 
    $returnValue["message"] = "Missing required information"; 
    echo json_encode($returnValue); 

    return; 
} 

$userEmail = htmlentities($_REQUEST["userEmail"]); 
$userPassword = htmlentities($_REQUEST["userPassword"]); 
$userFirstName = htmlentities($_REQUEST["userFirstName"]); 
$userLastName = htmlentities($_REQUEST["userLastName"]); 

$salt = openssl_random_pseudo_bytes(16); 
$secure_password = sha1($userPassword . $salt); 

echo $secure_password; 

$dao = new MySQLDAO(Conn::$dbhost, Conn::$dbuser, Conn::$dbpass, Conn::$dbname); 

$dao->openConnection(); 

$userDetails = $dao->getUserDetails($userEmail); 

if(!empty($userDetails)) 
{ 
    $returnValue["status"] = "400"; 
    $returnValue["message"] = "Please choose different email address"; 
    echo json_encode($returnValue); 
    return; 
} 

$result = $dao->registerUser($userEmail, $userFirstName, $userLastName, $secure_password, $salt); 

if ($result) { 
    $userDetails = $dao->getUserDetails($userEmail); 
    $returnValue["status"] = "200"; 
    $returnValue["message"] = "Sucessfully registered new user"; 
    $returnValue["userId"] = $userDetails["user_id"]; 
    $returnValue["userFirstName"] = $userDetails["first_name"]; 
    $returnValue["userLastName"] = $userDetails["last_name"]; 
    $returnValue["userEmail"] = $userDetails["email"]; 

} else { 
    $returnValue["status"] = "400"; 
    $returnValue["message"] = "Could not register user with provided information"; 
} 

$dao->closeConnection(); 

echo json_encode($returnValue); 

?> 

我的DAO對象超出波紋管:

<?php 

class MySQLDAO { 

    private $dbpassword; 
    var $dbhost = null; 
    var $dbuser = null; 
    var $dbpass = null; 
    var $conn = null; 
    var $dbname = null; 
    var $result = null; 

    function __construct($dbhost, $dbuser, $dbpassword, $dbname) { 

     $this->dbhost = $dbhost; 
     $this->dbuser = $dbuser; 
     $this->dbpass = $dbpassword; 
     $this->dbname = $dbname; 
    } 


    public function openConnection() { 

     $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); 
     if (mysqli_connect_error()) 
      throw new Exception("Could not stabilish connection with database"); 

     $this->conn->set_charset("utf8"); 
    } 

    public function closeConnection() { 
     if ($this->conn != null) 
      $this->conn->close(); 

    } 

    public function getUserDetails($email){ 

     $returnValue = array(); 
     $sql = "select * from users where email= '".$email."'"; 
     $result = $this->conn->query($sql); 

     if ($result != null && (mysqli_num_rows($result) >= 1)){ 
      $row = $result->fetch_array(MYSQLI_ASSOC); 

      if (!empty($row)){ 
       $returnValue = $row; 
      } 
     } 

     return $returnValue; 
    } 



    public function registerUser($email, $first_name, $last_name, $password, $salt) { 


     $sql = "insert unto users set email=?, first_name=?, last_name=?, user_password=?, salt=?"; 
     $statement = $this->conn->prepare($sql); 

     if (!$statement){ 
      throw new Exception($statement->error); 
     } 

     $statement->bind_param("sssss", $email, $first_name, $last_name, $password, $salt); 
     $returnValue = $statement->execute(); 

     return $returnValue; 

    } 

} 

我的連接類

<?php 

class Conn { 

    public static $dbhost = "localhost"; 
    public static $dbuser = "root"; 
    public static $dbpass = ""; 
    public static $dbname = "SwiftApp"; 

} 
?> 
+2

哪條線是76? –

回答

2

還有一個可能的錯誤,我可以發現:

此SQL查詢有一個錯字,應該是into,不unto,這將導致語法錯誤:

$sql = "insert unto users set email=?, first_name=?, last_name=?, user_password=?, salt=?"; 
$statement = $this->conn->prepare($sql); 

的語法錯誤會導致$mysqli->prepare()返回false

如果是這種情況,下一個塊無法工作。

if (!$statement){ 
    throw new Exception($statement->error); 
} 

如果$statementfalse,它不是一個對象,所以$statement->error不工作和錯誤Trying to get property of non-object被拋出。

這應該報告期望的結果:

/// corrected query 
$sql = "insert into users set email=?, first_name=?, last_name=?, user_password=?, salt=?"; 
$statement = $this->conn->prepare($sql); 

if (!$statement){ 
    /// corrected error reporting 
    throw new Exception($this->conn->error); 
} 
+0

謝謝!有效! – GuiSoySauce