Значения переменных не могут попасть в базу данных

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. код php mysql

<?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 ответ

  1. [У вас есть только 2 заполнителя («ss»), но 6 переменных, в $statement->bind_param(«ss», $email, $password, $username, $fname, $lname, $mobile);. Вы, вероятно, хотите 6 — > «ssssss – — Sean Dec 31′ 16 в 6: 49]

    Шон опубликовал мне ответ