我正在尝试使用codeigniter和node.js为数据库中的推送通知做一个通知表,但是我做了一个数组,其中包含我的会话购物车中的产品ID,但我通过我的函数发送它们看起来像这是错误输出
INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES('low stock', 2, ('4169','4170','4173','4172','4175'), '1', 1499414552)
我怎么能一起发送,但不是因为我发送我想发送我的ID然后将它们与我的通知表一起存储我的代码看起来像这样
通知功能
public function index()
{
$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->json($product_id);
$this->notification->addNotification('low stock', $product_id, $this->session->log['id'], 'low stock',now());
}
模型
插入通知
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 {
if ($this->db->query($sql, array($message, $type, $product_id, $user_id, $timestamp))) {
return true;
}else{
return false;
}
} catch (Exception $e) {
}
}else{
return true;
}
}
答案 0 :(得分:0)
也许你可以写这样的SQL:
insert into storelte_notifications (message,type,product_id,user_id,timestamp)
values ('low stock', 2, '4169', '1', 1499414552),('low stock', 2, '4170', '1', 1499414552)
或者您可以使用foreach
答案 1 :(得分:0)
使用foreach
循环:
$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) {
}