我遵循插入函数,使用mysqli prepare将值插入数据库。但这并没有给我任何错误,也没有在数据库中插入值。有人可以帮我解决问题吗?
我可以打印所有这些数组$fields, $placeholders, $values, $format
,它们也都显示正确的值。
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;
}
答案 0 :(得分:0)
将$bp = call_user_func_array(array($stmt, "bind_param"), array_merge($format, $values));
语句更改为$bp = $stmt->bind_param($format, ...$values);