gpt4 book ai didi

php - 当我尝试插入到表中时,如何修复当前位置的错误?

转载 作者:行者123 更新时间:2023-11-29 18:38:28 25 4
gpt4 key购买 nike

我想在用户售出商品后执行此操作,但如果我的最终库存少于我的最小库存,那么它会从数据库中在我的表上插入一条通知,但在使用 node.js 推送该通知之后,但是当我尝试插入售后表时,显示这样的错误,我应该如何修复它?

public function concretar_venta(){
if($this->sale->checa_carrito_vacio($this->session->carrito)){
$total = $this->input->post("total", TRUE);
$cantidad_pagada = $this->input->post("cantidad_pagada", TRUE);
$cambio = $cantidad_pagada - $total;
if($this->sale->concretar_venta($this->session->carrito, $total, $cantidad_pagada, $cambio)){
$this->json(array('success' => 'The sale was successfully made'));
}
else{
$this->json(array('error' => 'There was an error making the sale, please try again'));
}

$this->session->carrito = $this->sale->checar_existe_carrito();
$array = $this->sale->get_all_cart($this->session->carrito);
$product_id = array();
foreach ($array as $key => $value) {
$product_id[] = $value['id'];
}

$this->notification->addNotification('low stock', $product_id, $this->session->log['id'], 'low stock');

/*if ($product->stock <= 8) {
$this->notification->addNotification('low stock', $product_id, $this->session->log['id'], 'low stock');
} else {
# code...
}*/

}
else{
$this->json(array('error' => 'The cart is empty'));
}
}

模型通知:

public function addNotification($message, $product_id, $user_id, $type = ''){
$types = array('new' => 0, 'pending' => 1, 'low stock' => 2);
if (isset($types[$type]) === false) {
throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}
$type = $types[$type];
$timestamp = time();
$query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN ? AND type = ? ";
$previousNotification = $this->db->query($query, array($product_id, $type))->result_array();
if ($previousNotification[0]['notificationCount'] == 0) {
$sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
try {
foreach ($product_id as $pid) {
if (!$this->db->query($sql, array($message, $type, $pid, $user_id, $timestamp))) {
return false;
}
}
return true;
} catch (Exception $e) {

}
}else{
return true;
}
}

错误输出:

Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') AND type = 2' at line 1 SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN () AND type = 2 Filename: C:/xampp/htdocs/storelte/system/database/DB_driver.php Line Number: 691

最佳答案

IN() 需要 checkin 一串 Product_ids,因此您需要将 Product_ids 数组转换为字符串

这两行之间:

 $timestamp = time();
$query = "SELECT COUNT(*)...

添加

 $timestamp = time();
$product_id = implode(',',$product_id);//add this line
$query = "SELECT COUNT(*)...

http://php.net/manual/en/function.implode.php

关于php - 当我尝试插入到表中时,如何修复当前位置的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45088748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com