如何从数据库Codeigniter扣除值

时间:2019-09-18 04:23:06

标签: php mysql codeigniter

我目前在将数据库中的值扣除到我的字段时遇到问题

这是我的代码

在我的模型中,我有一个称为deduct

的函数。
public function deduct($id){

        $data = array(
            'loan' => $this->input->post('loan'),
            'id' => $id,
            'loan_type' => $this->input->post('loan_type'),
            'loan_amount' => $this->input->post('loan_amount'),
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'middlename' => $this->input->post('middlename'),
            'gender' => $this->input->post('gender'),
            'civilstatus' => $this->input->post('civilstatus'),
            'birthday' => $this->input->post('birthday'),
            'age' => $this->input->post('age'),
            'dependents' => $this->input->post('dependents'),
            'spousefirstname' => $this->input->post('spousefirstname'),
            'spouselastname' => $this->input->post('spouselastname'),
            'spousemiddlename' => $this->input->post('spousemiddlename'),
            'spouseage' => $this->input->post('spouseage'),
            'spousebirthday' => $this->input->post('spousebirthday'),
            'homeaddress' => $this->input->post('homeaddress'),
            'zipcode' => $this->input->post('zipcode'),
            'lengthofstay' => $this->input->post('lengthofstay'),
            'hometype' => $this->input->post('hometype'),
            'emailaddress' => $this->input->post('emailaddress'),
            'homephonenumber' => $this->input->post('homephonenumber'),
            'businessphonenumber' => $this->input->post('businessphonenumber'),
            'mobilenumber' => $this->input->post('mobilenumber'),
            'nameofbusiness' => $this->input->post('nameofbusiness'),
            'natureofbusiness' => $this->input->post('natureofbusiness'),
            'addressofbusiness' => $this->input->post('addressofbusiness'),
            'yearsofbusiness' => $this->input->post('yearsofbusiness'),
            'nameofcomaker' => $this->input->post('nameofcomaker'),
            'addressofcomaker' => $this->input->post('addressofcomaker'),
            'numberofcomaker' => $this->input->post('numberofcomaker'),
            'balance' => $this->input->post('cash_amount')
        );


        $this->db->set('loan_amount','loan_amount-balance', false);
        $this->db->where('id', $id);
        $this->db->update('employee',$data);
    }

在我的控制器上,我有一个pay函数

public function pay($id)
    {
        $data = array();
        $this->template->set('title', 'Payment Section');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('cash_amount', 'cash_amount', 'required|callback_check_default');
        $this->form_validation->set_message('check_default','There are some fields that are required to fill up!');
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger" id="hideMe">', '</div>');
        if ($this->form_validation->run() === FALSE)
        {
            $data["data"] = $this->employee->get_employees('', '', $id);
            $this->template->load('cashier_dashboard', 'contents' , 'payment', $data);
        }   
        else
        {
            $this->employee->deduct($id);
            redirect(base_url(). "home/cashier_dashboard");
        }
    }

实际上,我有2个默认布局视图以及用于添加现金的表格

这是我的默认布局

<table class="table table-bordered table-hover" cellspacing="0" width="100%" id="employee">
        <thead>
            <tr>
                <th>Loan</th>
                <th>Loan Type</th>
                <th>Loan Amount</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Balance</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php $i=1;foreach($data as $rec): ?>
        <tr>
            <td><?php echo $rec->loan; ?></td>
            <td><?php echo $rec->loan_type; ?></td>
            <td><?php echo $rec->loan_amount; ?></td>
            <td><?php echo $rec->firstname; ?></td>
            <td><?php echo $rec->lastname; ?></td>
            <td><?php echo $rec->balance?></td>
            <td>
                <div class="hidden-sm hidden-xs btn-group">
                    <a class="btn btn-xs btn-success" href="<?php echo site_url('home/pay/'.$rec->id); ?>">
                        <i class="ace-icon fa fa-rub bigger-120"></i>
                    </a>
                </div>
            </td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

在我的表单视图上

<?php echo form_open('home/pay/'.$data['id'], "class='form-horizontal'"); ?>

    <div class=form-group>
        <div class="row">
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Loan" name="loan" value="<?php echo $data['loan']; ?>" readonly>
            </div>
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Loan Type" name="loan_type" value="<?php echo $data['loan_type']; ?>" readonly>
            </div>
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Loan Amount" name="loan_amount" value="<?php echo $data['loan_amount']; ?>" readonly>
            </div>
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Cash Amount" name="cash_amount" value="<?php echo set_value('cash_amount'); ?>">
            </div>
        </div>
    </div>

    <div class="space-4"></div>
    <div class="clearfix form-actions">
        <div class="col-md-offset-3 col-md-9">
            <button class="btn btn-info center-block" type="submit">
                <i class="ace-icon fa fa-check bigger-110"></i>
                Submit
            </button>

            &nbsp; &nbsp; &nbsp;
            <button class="btn" type="reset">
                <i class="ace-icon fa fa-undo bigger-110"></i>
                Reset
            </button>
        </div>
    </div>
</form>


在我的数据库中,我有一个balance字段和loan_amount字段 我需要根据我在视图上的loan_amount输入中输入的内容来推导cash_amount。但目前尚未扣除

1 个答案:

答案 0 :(得分:0)

$this->db->set('loan_amount','loan_amount-balance', false);无效,因为您在$data中设置了$this->db->update('employee',$data);

这将解决您的问题:)

public function deduct($id){

        $data = array(
            'loan' => $this->input->post('loan'),
            'id' => $id,
            'loan_type' => $this->input->post('loan_type'),
            'loan_amount' => $this->input->post('loan_amount') - $this->input->post('cash_amount'),
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'middlename' => $this->input->post('middlename'),
            'gender' => $this->input->post('gender'),
            'civilstatus' => $this->input->post('civilstatus'),
            'birthday' => $this->input->post('birthday'),
            'age' => $this->input->post('age'),
            'dependents' => $this->input->post('dependents'),
            'spousefirstname' => $this->input->post('spousefirstname'),
            'spouselastname' => $this->input->post('spouselastname'),
            'spousemiddlename' => $this->input->post('spousemiddlename'),
            'spouseage' => $this->input->post('spouseage'),
            'spousebirthday' => $this->input->post('spousebirthday'),
            'homeaddress' => $this->input->post('homeaddress'),
            'zipcode' => $this->input->post('zipcode'),
            'lengthofstay' => $this->input->post('lengthofstay'),
            'hometype' => $this->input->post('hometype'),
            'emailaddress' => $this->input->post('emailaddress'),
            'homephonenumber' => $this->input->post('homephonenumber'),
            'businessphonenumber' => $this->input->post('businessphonenumber'),
            'mobilenumber' => $this->input->post('mobilenumber'),
            'nameofbusiness' => $this->input->post('nameofbusiness'),
            'natureofbusiness' => $this->input->post('natureofbusiness'),
            'addressofbusiness' => $this->input->post('addressofbusiness'),
            'yearsofbusiness' => $this->input->post('yearsofbusiness'),
            'nameofcomaker' => $this->input->post('nameofcomaker'),
            'addressofcomaker' => $this->input->post('addressofcomaker'),
            'numberofcomaker' => $this->input->post('numberofcomaker'),
            'balance' => $this->input->post('cash_amount')
        );

        $this->db->where('id', $id);
        $this->db->update('employee',$data);
    }