2016-11-23 71 views
0

我需要触发器的帮助。我有两个表'产品'和'存储'。这个想法是当我在表'storage'的列'flag'上运行更新时,触发器必须为正确的ID计算库存,但总是返回所有产品的错误值,请参阅附加。正确的股票价值应该是'测试'10件和'aaaaaa'5件。感谢帮助。Mysql触发器返回坏股价值

result screen

产品表:

CREATE TABLE `products` (
    `id` int(11) NOT NULL, 
    `subcategory_id` int(11) NOT NULL, 
    `product_name` varchar(255) COLLATE utf8_bin NOT NULL, 
    `product_description` varchar(255) COLLATE utf8_bin NOT NULL, 
    `product_price` int(11) NOT NULL, 
    `product_type` varchar(1) COLLATE utf8_bin NOT NULL, 
    `product_quantity` int(11) NOT NULL 
) ENGINE=InnoDB; 

INSERT INTO `products` (`id`, `subcategory_id`, `product_name`, `product_description`, `product_price`, `product_type`, `product_quantity`) VALUES 
(45, 11, 'Test', 'test', 1111, 'G', 15), 
(46, 11, 'aaaaaa', 'aaaaaa', 12, 'G', 15); 

存储表:

CREATE TABLE `storage` (
    `id` int(11) NOT NULL, 
    `product_id` int(11) NOT NULL, 
    `flag` varchar(1) COLLATE utf8_bin NOT NULL DEFAULT 'A' 
) ENGINE=InnoDB; 

INSERT INTO `storage` (`id`, `product_id`, `flag`) VALUES 
(46, 45, 'A'), 
(47, 45, 'A'), 
(48, 45, 'A'), 
(49, 45, 'A'), 
(50, 45, 'A'), 
(51, 45, 'A'), 
(52, 45, 'A'), 
(53, 45, 'A'), 
(54, 45, 'A'), 
(55, 45, 'A'), 
(56, 46, 'A'), 
(57, 46, 'A'), 
(58, 46, 'A'), 
(59, 46, 'A'), 
(60, 46, 'A'); 

mysql的触发器:

DELIMITER $$ 
CREATE TRIGGER `tg_ai_table22` AFTER UPDATE ON `storage` FOR EACH ROW UPDATE products 
    SET products.product_quantity = (SELECT COUNT(product_quantity) 
FROM STORAGE, (SELECT id FROM products) as prod 
WHERE prod.id = storage.product_id AND storage.flag = 'A' LIMIT 1) 
$$ 
DELIMITER ; 

PHP函数: 车控制器:

public function addToCart() 
    { 
     $id = $this->uri->segment(3); 
     $data = $this->ProductModel->selectProductToCart($id); 
     $cartData = array(); 
     foreach ($data as $datas) { 
      $cartData = array(
       'id' => $datas->id, 
       'qty' => 1, 
       'price' => $datas->product_price, 
       'name' => $datas->product_name 
      ); 
     } 
     $this->cart->insert($cartData) ? $this->db->limit(1)->set('flag', 'C')->where('product_id', $cartData['id'])->where('flag', 'A')->update('storage') : ''; 
    } 

public function updateCart() 
    { 
     $updatedCartData = $this->input->post(); 

     for ($i = 1; $i <= sizeof($this->cart->contents()); $i++) 
     { 
      if ($this->cart->contents()[$updatedCartData[$i]['rowid']]['rowid'] == $updatedCartData[$i]['rowid']) 
      { 
       if ($this->cart->contents()[$updatedCartData[$i]['rowid']]['qty'] > $updatedCartData[$i]['qty']) 
       { 
        $result = $this->cart->contents()[$updatedCartData[$i]['rowid']]['qty'] - $updatedCartData[$i]['qty']; 
        for ($j = 1; $j <= $result; $j++) 
        { 
         $this->db->limit(1)->set('flag', 'A')->where('product_id', 
          $this->cart->contents()[$updatedCartData[$i]['rowid']]['id'])->where('flag', 
          'C')->update('storage'); 
        } 
       } else 
       { 
        $result = $updatedCartData[$i]['qty'] - $this->cart->contents()[$updatedCartData[$i]['rowid']]['qty']; 
        for ($j = 1; $j <= $result; $j++) 
        { 
         $this->db->limit(1)->set('flag', 'C')->where('product_id', 
          $this->cart->contents()[$updatedCartData[$i]['rowid']]['id'])->where('flag', 
          'A')->update('storage'); 
        } 
       } 
      } 
     } 

     $this->cart->update($updatedCartData); 
     $this->session->set_flashdata('category_success', 'Kosik bol aktualizovany.'); 
     redirect('Cart'); 
    } 

ProductController的:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

class Product extends MY_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('ProductModel'); 
    } 

    public function index($id) 
    { 
     $data['product'] = $this->ProductModel->selectProduct($id); 
     $this->load->view('HeaderView'); 
     $this->load->view('UpperMenuView'); 
     $this->load->view('LeftMenuView'); 
     $this->load->view('ProductView', $data); 
     $this->load->view('FooterView'); 
    } 
} 

产品型号:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

class ProductModel extends CI_Model 
{ 
    private $table = 'products'; 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    function selectProduct($id) 
    { 
     return $this->db->get_where($this->table, array('subcategory_id' => $id))->result(); 
    } 

    function selectProductToCart($id) 
    { 
     return $this->db->get_where($this->table, array('id' => $id))->result(); 
    } 

    public function insertProduct($data) 
    { 
     $this->db->insert($this->table, $data); 
    } 

    public function insertProductToStorage($data) 
    { 
     $this->db->insert('storage', $data); 
    } 
} 

的ProductView:

<div class="col-md-9"> 
    <?php $this->load->view('FlashMessagesView'); ?> 
    <ul class="list-unstyled" id="products" data-role="list"> 
     <?php foreach ($product as $value): ?> 
      <li class="span3 col-md-3"> 
       <div class="thumbnail"> 
        <a href="product_details.html"><img src="<?php echo base_url('assets/img/12.jpg'); ?>"/></a> 
        <div class="caption" style="height: 300px; overflow: hidden"> 
         <h5><?php echo $value->product_name; ?></h5> 
         <p><?php echo $value->product_description; ?></p> 
        </div> 
        <div class="product_footer caption"> 
         <?php if ($value->product_quantity == 0) { ?> 
          <p style="text-align: center"><span style="color:orange"><b>Ordered on request.</b></span></p> 
         <?php } else { ?> 
          <p style="text-align: center"> 
           <span style="color:green"> 
            <b>In stock <?php echo $value->product_quantity ?> pcs.</b> 
           </span> 
          </p> 
         <?php } ?> 
         <h4> 
          <a type="button" href="<?php echo base_url('Cart') ?>" id="<?php echo $value->id ?>" 
           class="btn btn-success">Buy</a> 
          <span class="pull-right"><?php echo $value->product_price; ?> &euro;</span> 
         </h4> 
        </div> 
       </div> 
      </li> 
     <?php endforeach; ?> 
    </ul> 
</div> 
+1

你的php代码在哪里? –

+0

你好,现在我编辑了一个文章 – matus177

+0

在执行“插入”查询之前,运行antother查询来检查库存,例如执行查询后,从存储where product ='product_id'“的选择count(*),你可以简单地运行$ number_of_stock = $ query_res-> fetchColumn();并与此你有股票的数量 –

回答

1

你触发错写成,它是使更新所有产品不管o f id。

要改变只是为了用更新标志的产品更改更新语句触发以下:

UPDATE products 
SET products.product_quantity = 
    ( 
     SELECT COUNT(product_quantity) 
     FROM STORAGE, (SELECT id FROM products) as prod 
     WHERE prod.id = storage.product_id 
     AND storage.flag = 'A' LIMIT 1 
    ) 
where id = new.product_id 

,我们要做的就是将where id = new.product_id做出更新只是更新的产品标志。

而且,我认为您更新股票的方式并不是最优的,为什么每次更新时都重新计算股票?

+0

你好,谢谢你的帮助。这是因为我想如果我在购物车中有产品,则会自动从产品页面中统计此产品的数量(如您在第一张图片中看到的那样)。对于产品'测试'有10件和'aaaaaa'产品5件。 – matus177

+0

'flags'的值可能是什么? – developer

+0

的主要思想是:当产品可用标志为'A'并且数量显示在页面中时,如果有人按下购买按钮并且将产品插入购物车标志更新为'C'并且该产品的数量从全部数量中减少但是从购物车数量中删除这些产品时要计算回来。最后,当购物车中的表单被冒充并且产品被出售时,则获得标记值'S'并且不再可用。 – matus177