如何在我尝试插入表格时从哪里修复错误?

时间:2017-07-13 18:46:01

标签: php mysql codeigniter-3

我想一旦用户卖出了一件商品,但如果我的最终库存少于我的最小库存那么它会从我的数据库中插入一个通知我的表,但是之后用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语法出错;查看与您的MariaDB服务器版本对应的手册,以便在第1行使用正确的语法')AND type = 2'SELECT COUNT(*)AS notificationCount FROM storelte_notifications WHERE product_id IN()AND type = 2文件名:C:/ xampp / htdocs / storelte / system / database / DB_driver.php行号:691

1 个答案:

答案 0 :(得分:0)

IN()需要一串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