在codeigniter中的数据库中插入多个记录

时间:2017-09-22 08:02:37

标签: php mysql codeigniter

我是codeigniter的新手。我有一个表格式的表格。以表格格式记录我想在选择它后只更改选择框中的一条记录,我想点击多个记录的多个复选框和&当点击提交按钮时,记录应该需要添加到数据库表中。

表格:付款模式column我在其中更改了值。billIdcolumn有一个checkboxes。通过点击它我想提交数据。

Bill Id Reciept No. Product Id Amount Volume Rate Date Payment Mode     

1   416835       PETROL     50      1     76 19/06/2017 00:05:35    cash

2   416836  DIESEL  12000   203     59  19/06/2017 03:27:50     bank

3   416837  PETROL  400     5   76  19/06/2017 05:25:09     credit

模型:

function addNewsale($saleInfo, $cashInfo, $bankInfo, $creditInfo, $fleetInfo, $posInfo, $petrolInfo, $diselInfo)
{
    $billId = $this->input->post('billId');
    $productId = $this->input->post('productId');
    $amount = $this->input->post('amount');
    $volume = $this->input->post('volume');
    $rate = $this->input->post('rate');
    $date = $this->input->post('date');
    $paymentmod = $this->input->post('paymentmod');
    $vehicalno = $this->input->post('vehicalno');
    $recieptno = $this->input->post('recieptno');
    $payId = $this->input->post('payId');

    //$remainCredit = $creditAmount-$debitAmount;

    if($cashInfo){
        $this->db->insert('tbl_sale_detail',$saleInfo); 
        $billId=$this->db->insert_id();
        $cashInfo=array(
            'billId'=>$billId,
            'amount'=>$amount,
            'date'=>date('Y-m-d H:i:s'),
            'createdBy'=>$this->vendorId,
            'createdDtm'=>date('Y-m-d H:i:s'));
        $this->db->insert('tbl_cash_detail',$cashInfo);
    }

    if($bankInfo){
        $this->db->insert('tbl_sale_detail',$saleInfo);    
        $billId = $this->db->insert_id();
        $bankInfo = array(
            'billId'=>$billId,
            'amount'=>$amount,
            'date'=>date('Y-m-d H:i:s'),
            'createdBy'=>$this->vendorId,
            'createdDtm'=>date('Y-m-d H:i:s'));
        $this->db->insert('tbl_bank_detail',$bankInfo);
    }

    if($creditInfo){
        $this->db->insert('tbl_sale_detail',$saleInfo);    
        $billId=$this->db->insert_id();
        $creditInfo=array(
            'billId'=>$billId,
            'customerId'=>$customerId,
            'creditAmount'=>$amount,
            'debitAmount'=>$debitAmount,
            'remainCredit'=>$remainCredit,
            'date'=>date('Y-m-d H:i:s'),
            'createdBy'=>$this->vendorId,
            'createdDtm'=>date('Y-m-d H:i:s'));
        $this->db->insert('tbl_credit_detail',$creditInfo);
    }

    if($posInfo){
        $this->db->insert('tbl_sale_detail',$saleInfo);    
        $billId=$this->db->insert_id();
        $posInfo=array(
            'billId'=>$billId,
            'amount'=>$amount,
            'date'=>date('Y-m-d H:i:s'),
            'createdBy'=>$this->vendorId,
            'createdDtm'=>date('Y-m-d H:i:s'));
        $this->db->insert('tbl_pos_detail',$posInfo);
    }

    if($fleetInfo){
        $this->db->insert('tbl_sale_detail',$saleInfo);    
        $billId=$this->db->insert_id();
        $fleetInfo=array(
            'billId'=>$billId,
            'amount'=>$amount,
            'date'=>date('Y-m-d H:i:s'),
            'createdBy'=>$this->vendorId,
            'createdDtm'=>date('Y-m-d H:i:s'));
        $this->db->insert('tbl_fleet_detail',$fleetInfo);
    }

    if($diselInfo){
        $this->db->trans_start();
        $this->db->insert('tbl_disel_tank', $diselInfo);
        $this->db->where('purchaseId', $purchaseId);
        $insert_id = $this->db->insert_id();
        $this->db->trans_complete();
        return $insert_id;
    }
    if($petrolInfo){
        $this->db->trans_start();
        $this->db->insert('tbl_petrol_tank', $petrolInfo);
        $this->db->where('purchaseId', $purchaseId);
        $insert_id = $this->db->insert_id();
        $this->db->trans_complete();
        return $insert_id;
    }
}

控制器:

function addNewSale()
{
    if($this->isAdmin() == TRUE)
    {
        $this->loadThis();
    }
    else
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('productId','Product','trim');
        $this->form_validation->set_rules('amount','amount ','trim|required|numeric');
        $this->form_validation->set_rules('volume','volume','required');
        $this->form_validation->set_rules('rate','rate','trim|required');
        $this->form_validation->set_rules('date','date','required');
        $this->form_validation->set_rules('paymentmod','paymentmod ','trim|required');

        if($this->form_validation->run() == FALSE)
        {
            $this->addSale();
        }
        else
        {
            $billId = $this->input->post('billId');
            $productId = $this->input->post('productId');
            $amount = $this->input->post('amount');
            $volume = $this->input->post('volume');
            $rate = $this->input->post('rate');
            $date = $this->input->post('date');
            $paymentmod = $this->input->post('paymentmod');
            $vehicalno = $this->input->post('vehicalno');
            $recieptno = $this->input->post('recieptno');
            $payId = $this->input->post('payId');
            $petrolInfo = '';
            $diselInfo = '';
            $saleInfo = array(
                'billId'=>$billId,
                'recieptno'=>$recieptno,
                'productId'=>$productId,
                'amount'=>$amount,
                'volume'=>$volume,
                'rate'=>$rate,
                'date'=>$date,
                'paymentmod'=>$paymentmod,
                'vehicalno'=>$vehicalno,
                'createdBy'=>$this->vendorId,
                'createdDtm'=>date('Y-m-d H:i:s'));

            if($productId == 1){
                $last =  $this->db->order_by('petrolId',"desc")->get('tbl_petrol_tank')->row('totalVolume');
                print_r($last);

                $petrolInfo = '';
                $releaseVolume = $volume;
                $totalVolume = $last-$releaseVolume;
                $petrolInfo = array(
                    'productId'=>'petrol',
                    'productVolume'=>$volume,
                    'totalVolume'=>$totalVolume,
                    'releaseVolume'=>$releaseVolume,);
            }

            if($productId == 2){
                $lastd = $this->db->order_by('diselId',"desc")->get('tbl_disel_tank')->row('totalVolume');
                print_r($lastd);
                $diselInfo = '';
                $releaseVolume = $volume;
                $totalVolume = $lastd-$releaseVolume;
                $diselInfo = array(
                    'productId'=>'disel',
                    'productVolume'=>$volume,
                    'totalVolume'=>$totalVolume,
                    'releaseVolume'=>$releaseVolume,
                    'date'=>$date,
                    'createdBy'=>$this->vendorId,
                    'createdDtm'=>date('Y-m-d H:i:s'));
            }
            $cashInfo = '';
            $bankInfo = '';
            $creditInfo = '';
            $posInfo = '';
            $fleetInfo = '';
            $creditAmount = $amount;
            $debitAmount = $creditAmount-$amount;
            $remainCredit = $creditAmount-$debitAmount;

            if($payId == '1'){
                $cashInfo = array(
                    'billId'=>$billId,
                    'amount'=>$amount,
                    'date'=>date('Y-m-d H:i:s'),
                    'createdBy'=>$this->vendorId,
                    'createdDtm'=>date('Y-m-d H:i:s'));
            } 

            if($payId == '2'){
                $bankInfo = array(
                    'billId'=>$billId,
                    'amount'=>$amount,
                    'date'=>date('Y-m-d H:i:s'),
                    'createdBy'=>$this->vendorId,
                    'createdDtm'=>date('Y-m-d H:i:s'));
            }
            if($payId == '3'){
                $creditInfo = array(
                    'billId'=>$billId,
                    'customerId'=>$customerId,
                    'creditAmount'=>$amount,
                    'debitAmount'=>$debitAmount,
                    'remainCredit'=>$remainCredit,
                    'date'=>date('Y-m-d H:i:s'),
                    'createdBy'=>$this->vendorId,
                    'createdDtm'=>date('Y-m-d H:i:s'));
            }

            if($payId == '5'){
                $posInfo = array(
                    'billId'=>$billId,
                    'amount'=>$amount,
                    'date'=>date('Y-m-d H:i:s'),
                    'createdBy'=>$this->vendorId,
                    'createdDtm'=>date('Y-m-d H:i:s'));
            }

            if($payId == '4'){
                $fleetInfo = array(
                    'billId'=>$billId,
                    'amount'=>$amount,
                    'date'=>date('Y-m-d H:i:s'),
                    'createdBy'=>$this->vendorId,
                    'createdDtm'=>date('Y-m-d H:i:s'));
            }

            $this->load->model('sale_model');
            $result = $this->sale_model->addNewSale($saleInfo, $cashInfo, $bankInfo, $creditInfo, $posInfo, $fleetInfo, $petrolInfo, $diselInfo);

            if($result > 0)
            {
                $this->session->set_flashdata('success', 'New Sale created successfully');
            }
            else 
            {
                $this->session->set_flashdata('error', 'Disel Tank Is Empty');
            }

            redirect('index.php/addSale');
        }
    }
}

观点:

<?php

$billId = '';
$recieptno = '';
$productId = '';
$amount = '';
$volume = '';
$rate = '';
$date = '';
$paymentmod = '';
$vehicalno = '';

if(!empty($saleInfo))
{
    foreach ($saleInfo as $uf)
    {
        $billId = $uf->billId;
        $recieptno = $uf->recieptno;
        $productId = $uf->productId;
        $amount = $uf->amount;
        $volume = $uf->volume;
        $rate = $uf->rate;
        $date = $uf->date;
        $paymentmod = $uf->paymentmod;
        $vehicalno = $uf->vehicalno;
    }
}
?>

<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-inr"></i> Sale Management<small>Add </small>
    </h1>
  </section>
  <section class="content">
    <div class="row">
      <div class="col-xs-12 text-right">
        <div class="form-group">
          <!--<a class="btn btn-primary" href="<?php echo base_url();      ?>index.php/addSale"><i class="fa fa-plus"></i> Add Sale</a>-->
        </div>
      </div>
    </div>
    <div class="form-group">
    </div>

    <div class="row">
      <div class="col-xs-12">
        <div class="box">
          <div class="box-header">
            <h3 class="box-title">Sale List</h3>
            <div class="box-tools">
              <form role="form" action="<?php echo base_url() ?>index.php/addSale" method="post" id="addSale" role="form">
                <div class="input-group">
                  <input type="text" name="searchText" value="<?php echo $searchText; ?>" class="form-control input-sm pull-right" style="width: 150px;" placeholder="Search"/>
                  <div class="input-group-btn">
                    <button class="btn btn-sm btn-default searchList"><i class="fa fa-search"></i></button>
                  </div>
                </div>
              </div>
            </div><!-- /.box-header -->

            <div class="box-body table-responsive no-padding">
              <table class="table table-hover">
                <tr>
                  <th>Reciept No.</th>
                  <th>Product Id</th>
                  <th>Amount</th>
                  <th>Volume</th>
                  <th>Rate</th>
                  <th>Date</th>
                  <th>Payment Mode</th>
                  <th>Vehical No</th>
                </tr>
                <?php
                if(!empty($saleRecords))
                {
                    foreach($saleRecords as $key=>$value)
                    {
                ?>
                <tr>
                  <td><?php echo $value->recieptno ?></td>
                  <td><?php echo $value->productId ?></td>
                  <td><?php echo $value->amount ?></td>
                  <td><?php echo $value->volume ?></td>
                  <td><?php echo $value->rate ?></td>
                  <td><?php echo $value->date ?></td>
                  <td>
                    <select class="form-control required" id="paymentmod" name="paymentmod[]">
                      <option value="0">select</option>
                      <?php
                      if(!empty($paytypes))
                      {
                          foreach ($paytypes as $pl)
                          {
                          ?>
                      <option value="<?php echo $pl->payId ?>"><?php echo $pl->payType ?></option>
                      <?php
                          }
                      }
                      ?>
                    </select>
                  </td>
                  <td><?php echo $value->vehicalno ?></td>
                </tr>
                <?php
                    }
                }
                ?>
              </table>

              <div class="box-footer">
                <input type="submit" class="btn btn-primary" value="Submit" />
                <input type="reset" class="btn btn-default" value="Reset" />
              </form>
            </div><!-- /.box-body -->
            <div class="box-footer clearfix">
                <?php echo $this->pagination->create_links(); ?>
            </div>
          </div><!-- /.box -->
        </div>
      </div>
    </section>
  </div>

1 个答案:

答案 0 :(得分:0)

Codeigniter Query Builder有一个函数set-content

insert_batch
  

//生成:INSERT INTO mytable(标题,名称,日期)VALUES('我的   标题','我的名字','我的约会'),('另一个标题','另一个名字',   '另一个约会')

相关问题