变量值无法进入数据库

时间:2016-12-31 06:33:01

标签: php mysql json

1。无法通过注册过程获取用户详细信息

请找到注册用户代码:

<?php 
require("Conn.php");
require("MySQLDao.php");

$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$username = htmlentities($_POST["username"]);
$fname = htmlentities($_POST["fname"]);
$lname = htmlentities($_POST["lname"]);
$mobile = htmlentities($_POST["mobile"]);

$returnValue = array();

if(empty($email) || empty($password) || empty($username) || empty($fname) ||     empty($lname) || empty($mobile)) {
    $returnValue["status"] = "error";
    $returnValue["message"] = "Missing required field";
    echo json_encode($returnValue);
    return;
}

$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);

if(!empty($userDetails)) {
    $returnValue["status"] = "error";
    $returnValue["message"] = "User already exists";
    echo json_encode($returnValue);
    return;
}

$secure_password = md5($password); // I do this, so that user password     cannot be read even by me

$result = $dao-    >registerUser($email,$secure_password,$username,$fname,$lname,$mobile);

if($result) {
    $returnValue["status"] = "Success";
    $returnValue["message"] = "User is registered";
    echo json_encode($returnValue);
    return;
}
$dao->closeConnection();
?>

2。用户登录代码

<?php
require("Conn.php");
require("MySQLDao.php");
$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$returnValue = array();

if(empty($email) || empty($password)) {
    $returnValue["status"] = "error";
    $returnValue["message"] = "Missing required field";
    echo json_encode($returnValue);
    return;
}
$secure_password = md5($password);

$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetailsWithPassword($email,$secure_password);

if(!empty($userDetails)) {
    $returnValue["status"] = "Success";
    $returnValue["message"] = "User is registered";
    echo json_encode($returnValue);
} else {
    $returnValue["status"] = "error";
    $returnValue["message"] = "User is not found";
    echo json_encode($returnValue);
}
$dao->closeConnection();
?>

3。 mysql php代码

<?php
class MySQLDao {
    var $dbhost = null;
    var $dbuser = null;
    var $dbpass = null;
    var $conn = null;
    var $dbname = null;
    var $result = null;

    function __construct() {
        $this->dbhost = Conn::$dbhost;
        $this->dbuser = Conn::$dbuser;
        $this->dbpass = Conn::$dbpass;
        $this->dbname = Conn::$dbname;
    }

    public function openConnection() {
        $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
        if (mysqli_connect_errno())
            echo new Exception("Could not establish connection with database");
    }

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

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

    public function getUserDetails($email) {
        $returnValue = array();
        $sql = "select * from ap_users where user_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 getUserDetailsWithPassword($email, $userPassword) {
        $returnValue = array();
        $sql = "select id,user_email from ap_users where user_email='" . $email. "' and user_password='" .$userPassword . "'";

        $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, $password, $username, $fname, $lname, $mobile) {
        $sql = "insert into ap_users set user_email=?, user_password=?, user_username=?, user_fname=?, user_lname=?, user_mobile=?";
        $statement = $this->conn->prepare($sql);

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

        $statement->bind_param("ss", $email, $password, $username, $fname, $lname, $mobile);
        $returnValue = $statement->execute();
        return $returnValue;
    }
}
?>

4。我无法解决的问题

当我尝试通过应用程序注册用户时没有响应。没有任何值写入数据库。

1 个答案:

答案 0 :(得分:0)

[你只有2个占位符(“ss”),但是6个变量,在$ statement-&gt; bind_param(“ss”,$ email,$ password,$ username,$ fname,$ lname,$ mobile); 。你可能想要6 - &gt; “ssssss” - 肖恩2016年12月31日6:49]

肖恩给我发了答案