Codeigniter on insert根据所选的单选按钮更新其他表

时间:2017-04-29 05:47:09

标签: php mysql codeigniter

我需要一点Codeigniter / php / Mysql帮助,我有3个mysql表,项目,批处理和事务。在事务中,我有一个列transaction_type,在我的代码中我有4个单选按钮:Incoming,Outgoing,Damaged和Expired。 我还在交易,批次和项目表中有一个列数量,如果选择了单选按钮Incoming,我想要插入到交易表中,然后通过在两个表项目和批次中添加列数量来更新字段数量中的值。如果选择单选按钮Outgoing或Damaged or Expired,则通过减去两个表项和批次中的列数来更新字段数量中的值。

这是我的插入脚本

    if ($this->session->userdata('admin_login') != 1)
        redirect('login', 'refresh');
    if ($param1 == 'create') {

    $data['transaction_date'] = $this->input->post('transaction_date');
    $data['item'] = $this->input->post('item');
    $data['batch'] = $this->input->post('batch');
    $data['section'] = $this->input->post('section');
    $data['transaction_type'] = $this->input->post('transaction_type');
    $data['quantity'] = $this->input->post('quantity');
        $this->db->insert('transactions', $data);
      if($transaction_type == Incoming) {
        $prepared = $this->prepare("UPDATE items SET balance = quantity+$balance WHERE id=?");
        $this->execute($prepared, '');
    }elseif($transaction_type == Outgoing){
        $prepared = $this->prepare("UPDATE items SET balance = quantity-$balance WHERE id=?");
        $this->execute($prepared, '');
    }
     $this->session->set_flashdata('flash_message' , get_phrase('items_issued_successfully'));

这是我的表格

CREATE TABLE `batches` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item` int(10) unsigned DEFAULT NULL,
`supplier` int(10) unsigned DEFAULT NULL,
`batch_no` varchar(40) DEFAULT NULL,
`manufacturing_date` date DEFAULT NULL,
`expiry_date` date DEFAULT NULL,
`balance` decimal(10,2) DEFAULT '0.00',
PRIMARY KEY (`id`),
KEY `item` (`item`),
KEY `supplier` (`supplier`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

CREATE TABLE `items` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category` int(10) unsigned DEFAULT NULL,
`item` varchar(40) DEFAULT NULL,
`code` varchar(40) DEFAULT NULL,
`balance` decimal(10,2) DEFAULT '0.00',
`reorder_level` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `category` (`category`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8


CREATE TABLE `transactions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`transaction_date` varchar(255) DEFAULT NULL,
`item` int(10) unsigned DEFAULT NULL,
`batch` int(10) unsigned DEFAULT NULL,
`section` int(10) unsigned DEFAULT NULL,
`transaction_type` varchar(40) NOT NULL,
`quantity` decimal(10,2) DEFAULT '1.00',
PRIMARY KEY (`id`),
KEY `item` (`item`),
KEY `batch` (`batch`),
KEY `section` (`section`)
) ENGINE=InnoDB AUTO_INCREMENT=63 DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:0)

可能会在您使用thymeleaf进行检查但发布$transaction_type

中存储的数据的情况下发布

试试这个:

$data['transaction_type']