无法使用多个$ mysqli-> prepare将数据插入mysql

时间:2014-10-07 11:48:59

标签: php mysql prepared-statement

这是我的class.user.php写的: `

class User{

    private $db;
    public function __construct(){  
        $this->connect();

        }
        private function connect($db_connect=true){ 
            if ($db_connect){
                $this->db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
                if(mysqli_connect_errno()) {        
                    printf("DB Connect failed: %s\n", mysqli_connect_error());
                    exit;
            }
        }       
    }

    /*** for registration process ***/
    public function reg_user($name,$nickname,$gender,$birthdate,$address,$email,$short_info,$username,$password){
        // Get the MySQLi object
        $db = $this->db;
        if(empty($db)){
            $this->connect();
            $db = $this->db;
        }
        $password = md5($password);
        $stmt = $this->db->prepare("SELECT * FROM `user` WHERE `username` = ? `enter code here`or `email` = ?");
        $stmt = $bind_param('ss', $username, $email);
        $stmt->execute();

        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        $count_row = $result->num_rows;

        if ($count_row == 0){
            $stmt = $this->db->prepare("INSERT INTO `user` (`name`,`nickname`,`gender`,`birthdate`,`address`,`email`,`short_info`,`username`,`password`)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->bindParam('ssissssss', $name,$nickname,$gender,$birthdate,$address,$email,$short_info,$username,$password);
            $stmt->execute();   
            return $stmt;
        }else {return false;}

        mysqli_close($db);
        $this->db = null;
    }
 } `

这是我的signup.php:

<?php
error_reporting(0);
include_once '../include/class.user.php';
//$user = new User();

if (isset($_REQUEST['submit'])){
    extract($_REQUEST);
    $register = User::reg_user($uname,$alias,$gnder,$bdate,$addr,$e_mail,$shortinfo,$usrname,$passwrd);
    if ($register) {
        $msg = 'Registration  successful <a href="../index.php">Click here</a> to login';
    } else {
        $msg = 'Registration failed. Email or Username already exits please try again';
    }
}

问题是它没有错误并显示白屏,但无法插入数据库。

我认为这是因为多次查询但是我要做什么?

任何建议。

1 个答案:

答案 0 :(得分:0)

问题似乎在这里:

$stmt = $this->db->prepare("SELECT * FROM `user` WHERE `username` = ? or `email` = ?");
$stmt = $bind_param('ss', $username, $email)
$stmt->execute();

应该是:

$stmt = $this->db->prepare("SELECT * FROM `user` WHERE `username` = ? or `email` = ?");
$stmt->bind_param('ss', $username, $email);
$stmt->execute();

还有:

$stmt->bindParam('ssissssss', $name,$nickname,$gender,$birthdate,$address,$email,$short_info,$username,$password);

应该是:

$stmt->bind_param('ssissssss', $name,$nickname,$gender,$birthdate,$address,$email,$short_info,$username,$password);