php - 变量值无法进入数据库

标签 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。我自己无法解决的问题

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

最佳答案

[$statement->bind_param("ss", $email, $password, $username, $fname, $lname, $mobile); 中只有 2 个占位符 ("ss"),但有​​ 6 个变量。你可能想要 6 -> “ssssss” – Sean 2016 年 12 月 31 日 6:49]

肖恩已将答案发布给我

关于php - 变量值无法进入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406072/

相关文章:

javascript - 获取包含 # 值的 URL

mysql - 使用动态 SQL 将行转换为列

javascript - 使用成员函数扩展 React.js 存储数据结构?

c++ - C++中使用json-spirit读取json字符串

json - 将包Elastic(嵌套列表?)的R输出转换为data.frame或JSON

php - 如何在 redirecturl 中显示 Mollie 订单状态?

php从字符串范围中删除多余的单词

php - 一般错误 : 1 no such table: users in Laravel Unit Test

php - 传递变量和 php

mysql - 如何在 Spring MVC 应用程序中使用 Solr