doukan3504 2015-07-20 05:57
浏览 23
已采纳

注册用户PHP

I am trying to create a PHP script to register users. It connects my xCode with a mySQL database.

I am getting the following error:

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

This is my registerUser script

<?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);

?>

My DAO object goes bellow:

<?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;

    }

}

My connection class

<?php

class Conn {

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

}
?>
  • 写回答

1条回答 默认 最新

  • doune1000 2015-07-20 06:26
    关注

    There is one possible error I can spot:

    This SQL query has a typo, it should be into, not unto, resulting in a syntax error:

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

    The syntax error results in $mysqli->prepare() returning false.

    If this is the case the next block can't work.

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

    If $statement is false, it isn't an object, so $statement->error doesn't work and the error Trying to get property of non-object is thrown.

    This should report the desired result:

    /// 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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!