2017-07-13 40 views
0

我想一旦用戶已經出售了一件物品,但是如果我的最終庫存少於我的最小庫存,那麼它會從數據庫中向我的表插入通知,但是之後用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; 
     } 
    } 

錯誤輸出:

錯誤編號:1064您的SQL語法錯誤; SELECT AND COUNT(*)AS notificationCount FROM storelte_notifications WHERE product_id IN()AND type = 2文件名:C:/ XAMPP/htdocs中/ storelte /系統/數據庫/ DB_driver.php行號:691

+0

'IN()'需要參數 – WheatBeak

+0

OU應該BACKTICK列名 – GrumpyCrouton

+1

它看起來像你想的陣列'$ product_id'傳入的參數。這是行不通的。您必須執行一些操作才能檢查該陣列中的每個product_id。 – aynber

回答

0

IN()需要product_ids的字符串來檢查,所以你需要你的product_ids數組轉換爲字符串

之間這些行:

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

add

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

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

相關問題