Hi, I've got a problem here where I can't get the script to repeat something that I have successfully accomplished some place else.
At my register form, after all the validation and checks have being passed, I'm trying to insert the information into the database to register the user... but this is my first time using MySQLi, especially prepare and bind_params so I'm very rusty on this so far... can't seem to get it right the first time for some reason.
Anyway, I have a function in my MySQLi database access wrapper that uses the passed arguments to create the INSERT SQL and execute it... actually insert it. I have this function be used for all of the situations (which means the bind parameters would be different from time to time and the function needs to accept a dynamic amount of parameters). Here is my insert function.
public function insert($tbl_name, $fields, $values, $types, $vars)
{
$sql = "INSERT INTO `$tbl_name`";
$sql .= " (`" . implode("`, `", $fields) . "`)";
$sql .= " VALUES ('" . implode("', '", $values) . "')";
$stmt = $this->mysqli_link->stmt_init();
$this->last_sql = $sql;
$stmt->prepare($sql) or die($stmt->error);
$array = array(array($types), $vars);
$array = call_user_func_array('array_merge', $array);
$refArray = $this->make_ref($array);
$ref = new ReflectionClass('mysqli_stmt');
$method = $ref->getMethod('bind_param');
$method->invokeArgs($stmt, $refArray);
if(!$stmt->execute())
{
throw die($stmt->error);
}
return true;
}
Here is how I use it
$values = array('null',
'null',
'1',
'1',
'?',
'?',
'null',
'?',
'?',
time()
);
$vars = array($vars['form']['username'],
$pass,
$vars['form']['email'],
$dob
);
$stmt = $db->insert(USER_TABLE, $user_table_schema, $values, 'sssi', $vars);
And I'm getting the following error!
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\www\cms\includes\mmysqli.php on line 406
$refArray shows as:
Array
(
[0] => sssi
[1] => userName
[2] => passWord
[3] => email@somesite.com
[4] => 651189600
)
The thing is, I'm doing this exact method to bind_params elsewhere and it works well there. Here is the function that I'm using this thing at:
public function build_query($sql)
{
if(!is_array($sql))
{
if($this->silent === false)
{
trigger_error('The variable $sql is not an array ', E_USER_ERROR);
}
return false;
}
$where = array(null,null,array(),null);
if(isset($sql[2]) && is_array($sql[2]))
{
$where = $this->build_where($sql[2]);
}
$orderby = ((isset($sql[3])) ? ' ORDER BY ' . trim($sql[3]) : null);
$limit = ((isset($sql[4])) ? ' LIMIT ' . trim($sql[4]) : null);
$sql = "SELECT {$sql[0]} FROM `{$sql[1]}`{$where[0]}{$orderby}{$limit}";
if($this->valid_query($sql))
{
$stmt = $this->mysqli_link->stmt_init();
$this->last_sql = $sql;
$stmt->prepare($sql) or die($stmt->error);
if(!empty($where[2]))
{
$array = array(array($where[1]), $where[2]);
$array = call_user_func_array("array_merge", $array);
$refArray = $this->make_ref($array);
$ref = new ReflectionClass('mysqli_stmt');
$method = $ref->getMethod("bind_param");
$method->invokeArgs($stmt, $refArray);
}
$this->last_stmt = $stmt;
return $stmt;
}
else
{
return false;
}
}
It works in build_query() but not in insert()!
I have 4 question marks (for binding), 4 types (sssi) and 4 parameters (types and parameters in $refArray)... everything matches... maybe MySQLi forgot how to do math?