I've following insert function to insert values in to database using mysqli prepare. But this is not giving me any error and not inserting values in to database. Can somebody help me to troubleshoot?
I can print all these arrays $fields, $placeholders, $values, $format
and it's all displaying correct values too.
public function insert($table, $data, $format) {
global $dbcon;
// Check for $table or $data not set
if (empty($table) || empty($data)) {
return false;
}
// Cast $data and $format to arrays
$data = (array) $data;
$format = (array) $format;
// Build format string
$format = implode('', $format);
$format = str_replace('%', '', $format);
list( $fields, $placeholders, $values ) = $this->prep_query($data);
// Prepend $format onto $values
// array_unshift($values, $format);
// Prepary our query for binding
$stmt = $dbcon->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");
if (false === $stmt) {
die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}
// Dynamically bind values
$bp = call_user_func_array(array($stmt, "bind_param"), array_merge($format, $values));
// $bp = call_user_func_array(array($stmt, 'bind_param'), $this->ref_values($values));
if (false === $bp) {
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}
// Execute the query
$result = $stmt->execute();
if (false === $result) {
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
// Check for successful insertion
$Insertid = $stmt->insert_id;
return $Insertid;
// if ($stmt->affected_rows) {
// return true;
// }
//
// return false;
}