public function insert_new_listing($listing_assoc_array) {
global $public_connection;
$date = (string) date("Y-m-d");
$hit_count = 0;
if ($stmt = new mysqli_stmt($public_connection, 'INSERT INTO laptops (brand_name, title, description, price, discount, last_change_date, hit_count) VALUES (?,?,?,?,?,?,?)')) {
/* bind parameters for markers */
$stmt->bind_param(
"sssdisi",
$listing_assoc_array['brand_name'],
$listing_assoc_array['title'],
$listing_assoc_array['description'],
$listing_assoc_array['price'],
$listing_assoc_array['discount'],
$date,
$hit_count
);
/* execute query */
$stmt->execute();
我收到错误消息:变量数与准备好的语句中的参数数不匹配。
我在prepare语句和bind_param中具有正确的number(7),所以我不知道为什么会这样。
正如我在上面的评论中提到的那样,不可能new mysqli_stmt()
返回假值。您应该改为使用该mysqli::prepare
方法,例如...
public function insert_new_listing(mysqli $conn, array $listing) {
$stmt = $conn->prepare('INSERT INTO laptops (brand_name, title, description, price, discount, last_change_date, hit_count) VALUES (?,?,?,?,?,NOW(),0)');
if (!$stmt) {
throw new Exception($conn->error, $conn->errno);
}
/* bind parameters for markers */
$stmt->bind_param('sssdi',
$listing['brand_name'],
$listing['title'],
$listing['description'],
$listing['price'],
$listing['discount']);
/* execute query */
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
您可能会注意到,我使用的是文字0
,NOW()
而不是绑定$hit_count
和$date
。无法看到绑定已知静态值的任何理由。
我也将mysqli
实例作为方法依赖项传入,而不是依赖于全局变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句