Stefano Mtangoo
Stefano Mtangoo

Reputation: 6560

What is going wrong here -MySQLi and Custom class

I'm making a custom user management class that inherits from DBManager class. The problem I have so far is executing queries using mysqli prepared statements. DB connects fine but then prepare statement fails. I have mysqli object as an attribute that is shared in the class. I'm new with this mysqli (all the times I used normal mysql). Below is the constructor and insertusers method that fails. Thanks for your hel, and I have searched for so long now, so forgive me if there is something like this somewhere. I couldn't find it. Thanks again

<?php

protected  $dbLink;
protected  $errorMsg = array();//Inherited by other functions

public function __construct() {
$this->dbLink = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
    $this->addError(mysqli_connect_error());
}
}

public  function insertUsers($arrayofValues) {
        //for inserting users in the database
        $sql = "INSERT INTO users(firstname, lastname, email, username, password) VALUES(?, ?, ?, ?, ?";
        if( $stmt = $this->dbLink->prepare($sql)) {
            $type = array("sssss");
            $params = array_merge($type, $arrayofValues);
            call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
            $stmt->execute();

            if($stmt==-1) {
                //query failed
                $this->addError(mysqli_connect_error());
                return false;
            }else {
                return true;
            }
        }
        else{
            $this->addError("Something went wrong before InsertUsers stmt could execute well");
        }
    }
?>

Upvotes: 1

Views: 210

Answers (1)

Quassnoi
Quassnoi

Reputation: 425411

Close the parens in INSERT:

INSERT INTO users(firstname, lastname, email, username, password) VALUES(?, ?, ?, ?, ?)
                                                                                      ^

It usually helps adding the exact error description into the code:

        $this->addError("InsertUsers failed: " . $this->dblink->error);

Upvotes: 2

Related Questions