2017-07-07 3 views
0

データベースからのプッシュ通知にcodeigniterとnode.jsを使用して通知用のテーブルを作成しようとしていますが、セッションカートから製品IDを含む配列を作成しましたが、エラー出力に配列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

多分あなたは、このような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ループを使用しますforeach

+0

foreach同じファイルにない場合はどのようにすればいいですか?また準備された文章を使用しています –

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) { 

} 
関連する問題