我试图让我的通知功能接收一个数组,但是当我尝试发送我的数据时,它会因以下错误消息而失败:
错误号:1064您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在#"' 4168'附近使用正确的语法。 AND type ='低库存''第1行SELECT COUNT(*)AS notificationCount FROM storelte_notifications WHERE product_id IN' 4168' AND type ='低库存'文件名:models / Notification.php
我的新功能(重写为接受数组):
public function addNotification($data){
$types = array('new' => 0, 'pending' => 1, 'low stock' => 2);
if (isset($types[$data['type']]) === false) {
throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}
$type = $types[$data['type']];
$timestamp = time();
$query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN ? AND type = ? ";
$previousNotification = $this->db->query($query, array($data['product_id'],$data['type']))->result_array();
if ($previousNotification[0]['notificationCount'] == 0) {
$sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
try {
foreach ($data['product_id'] as $pid) {
if (!$this->db->query($sql, array($data['message'], $data['type'], $pid, $data['user_id'], $timestamp))) {
return false;
}
}
return true;
} catch (Exception $e) {
}
}else{
return true;
}
}
我的旧功能(使用分开的参数):
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;
}
}
这个测试让我多次添加
public function addNotification(Array $data) {
/*$types = array('new' => 0, 'pending' => 1, 'low stock' => 2);
if (isset($types[$data['type']]) === false) {
throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}
$type = $types[$data['type']];
$query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id = ? AND type = ? ";
$previousNotification = $this->db->query($query, array($data['product_id'],$data['type']))->result_array();
if ($previousNotification[0]['notificationCount'] == 0) {
$sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
try {*/
//foreach ($data as $pid) {
//if( !$this->db->query($sql, array($data['message'], $data['type'], $data['product_id'], $data['user_id'], $data['timestamp'])) ) {
$sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
$this->db->query($sql, array($data['message'], $data['type'], $data['product_id'], $data['user_id'], $data['timestamp']));
//return false;
// }
//}
//return true;
//} catch (Exception $e) {
//}
//} else {
// return true;
//}
}
所以,我的新函数$data
参数如下所示:
$entryData = array(
'message' => 'low stock',
'product_id' => $value['id'],
'user_id' => $this->session->log['id'],
'type' => 'low stock'
);
我称之为:
$this->notification->addNotification($entryData);
有谁可以告诉我错误在哪里?
谢谢!
答案 0 :(得分:0)
right syntax to use near ''4168'
看起来您的user_id
SQL WHERE部件是使用附加撇号生成的,但预计不会出现在那里。
代码看起来很好,你可以试试这个代码队列吗?
首先,用这个替换你的新功能:
public function addNotification(Array $data) {
$types = array('new' => 0, 'pending' => 1, 'low stock' => 2);
if (isset($types[$data['type']]) === false) {
throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}
$type = $types[$data['type']];
$timestamp = time();
$query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN ? AND type = ? ";
$previousNotification = $this->db->query($query, array($data['product_id'],$data['type']))->result_array();
if ($previousNotification[0]['notificationCount'] == 0) {
$sql = "INSERT INTO storelte_notifications (message,type,product_id,user_id,timestamp) VALUES(?, ?, ?, ?, ?)";
try {
foreach ($data['product_id'] as $pid) {
if( !$this->db->query($sql, array($data['message'], $data['type'], $pid, $data['user_id'], $timestamp)) ) {
return false;
}
}
return true;
} catch (Exception $e) {
}
} else {
return true;
}
}
然后,尝试执行此操作:
$entryData = array(
'message' => 'low stock',
'product_id' => array($value['id']),
'user_id' => $this->session->log['id'],
'type' => 'low stock'
);
$this->notification->addNotification($entryData);
看看它是否有效。
答案 1 :(得分:0)
这是用codeigniter写的吗?利用Active Record
public function addNotification($data)
{
$types = array(
'new' => 0,
'pending' => 1,
'low stock' => 2
);
if (isset($types[$data['type']]) === false) {
throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}
$type = $types[$data['type']];
$timestamp = time();
$select = array(
"COUNT(*) AS notificationCount"
);
$where = array(
"type" => $data['type'])
);
$this->db->select($select);
$this->db->where($where);
$this->db->where_in('product_id', $data['product_id']);
$query = $this->db->get('storelte_notifications');
$previousNotification = $query->row();
if ($previousNotification->notificationCount > 0)
{
return true;
}
else
{
if ($data['product_id']) {
foreach ($data['product_id'] as $pid) {
$insert_fields = array(
"message" => $data['message'],
"type" => $data['type'],
"product_id"=> $pid,
"user_id" => $data['user_id'],
"timestamp" => time()
);
$this->db->insert('storelte_notifications', $insert_fields);
}
return true;
}
return false;
}
}
答案 2 :(得分:0)
在in子句中使用括号
SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id IN (?) AND type = ? ";