数据未正确插入

时间:2016-06-02 11:50:34

标签: mysql codeigniter

这里有两个名为services和payment的表,我有两个表格,用于插入值...当我输入数据到服务时,我得到两行名称id和customer_id,我想生成这些值到支付表,同时添加到相应的ID .. 这是我的服务表..

    id  code     customer_id    particulars     serialno    complaint   
    22  ORD00022    16             tv             100         ddfds     
    23  ORD00023    19             GH             565          gfdg     
    24  ORD00024    15             tv             122         sdfsd     
    25  ORD00025    16             cvbcv           5         tgtdfgfg   
    26  ORD00026    16             cvbcv           5         tgtdfgfg   

这是我的付款表

id      order_id    customer_id   actual_amount   paid_amount   balance     type
3          0            0              250.00       100.00       150.00     Cash
4          0            0              250.00       50.00        100.00     Cash
5          0            0              150.00       50.00        100.00     Credit
10         0            0              500.00       500.00       400.00     Credit
13         26           16             250.00       100.00       0.00       Cash

这是我的观点页面

<li> <a href="" class="make_payment" data-toggle="modal" data-target=".payment" id="<?php echo $row->id; ?>" customer-id="<?php echo $row->customer_id; ?>" >Make Payment</a> </li>
 <div class="modal payment">
   <div class="modal-dialog">
<form action="<?php echo base_url(); ?>account_control/make_payment" method="post" id="assign-form">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h4 class="modal-title">Make Payment</h4>
    </div>
    <div class="modal-body">
     <div class="box-body">
      <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Total Amount</label>

        <div class="col-sm-10">
          <input type="text" class="form-control" name="total_amount" id="total_amount" placeholder="Total Amount">
        </div>
        <div class="clearfix"></div> 
      </div>
      <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Paid Amount</label>

        <div class="col-sm-10">
          <input type="text" class="form-control" name="paid_amount" id="paid_amount" placeholder="Paid Amount">
        </div>
        <div class="clearfix"></div>
      </div> 
      <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Balance</label>

        <div class="col-sm-10">
          <input type="text" class="form-control" name="balance" id="balance" placeholder="Balance">
        </div>
        <div class="clearfix"></div> 
      </div>
      <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">Balance</label>
        <div class="col-sm-10">
          <select name="type" class="form-control">
            <option value="Cash">Cash</option>
            <option value="Credit">Credit</option>
          </select>
        </div>
        <div class="clearfix"></div> 
      </div>
      <input type="hidden" name="order_id" id="order_id">
      <input type="hidden" name="customer_id" id="customer_id">        
    </div>


    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <input type="submit" class="btn btn-primary" value="Pay">
    </div>
  </div><!-- /.modal-content -->
</form>

我的控制器看起来像这样..

public function make_payment()
{
    $status = $this->Account_model->make_payment();
    if($status)
    {
        $this->session->set_flashdata('message','Payment successful');
    }
    else
    {
        $this->session->set_flashdata('message','Payment Failed');
    }
    redirect('account_control/view_order');
}

我的模型看起来像这样

public function make_payment()
{  

    $paid = $this->input->post('paid_amount');
    $total = $this->input->post('total_amount');
    $balance = $this->input->post('balance');
    $customer_id = $this->input->post('customer_id');
    $order_id = $this->input->post('order_id');

    $data = array('order_id' => $order_id,
        'customer_id' => $customer_id,
        'actual_amount'=>$total,
        'paid_amount' => $paid,
        'balance' => $balance
    );

    if($this->db->insert('payment',$data))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

如果两者都包含在一个表单中,我现在正确地显示错误,就像这样

Error Number: 1048

Column 'order_id' cannot be null

INSERT INTO `payment` (`order_id`, `customer_id`, `actual_amount`, `paid_amount`, `balance`) VALUES (NULL, 'NULL', '100', '50', '50')

Filename: C:/wamp64/www/account/application/models/Account_model.php

Line Number: 196

这是我的输出视图,来自下面的makep付款操作显示模态

  S.No    Order #          Date   Particulars   Serial/IME No  Complaints    Action
   1       ORD00027   06/07/2016    led            11          sedsdf       make payment

   2       ORD00026   06/13/2016    cvbcv           5          tgtdfgfg     make payment

我在支付表中插入的最后一行是我假设我应该得到的值

2 个答案:

答案 0 :(得分:0)

模型中的

$paid = $this->input->post('paid_amount');
$total = $this->input->post('total_amount');
$balance = $this->input->post('balance');
$order_id_data = $this->input->post('order_id');
$customer_id_data = $this->input->post('customer_id ');
  if(order_id_data ==""){

       $order_id="";

 }



  if(customer_id_data ==""){

       $customer_id ="";

 }
$data = array('order_id' => $order_id,
    'customer_id' => $customer_id,
    'actual_amount'=>$total,
    'paid_amount' => $paid,
    'balance' => $balance
);

if($this->db->insert('payment',$data))
{
    return TRUE;
}
else
{
    return FALSE;
}

试试这个

答案 1 :(得分:0)

您是否检查过以确保$ data数组实际包含order_id的值? <或者

    $data = array('order_id' => $order_id,
        'customer_id' => $customer_id,
        'actual_amount'=>$total,
        'paid_amount' => $paid,
        'balance' => $balance
    );
print_r($data);

    $data = array('order_id' => $order_id,
        'customer_id' => $customer_id,
        'actual_amount'=>$total,
        'paid_amount' => $paid,
        'balance' => $balance
    );
log_message('error', print_r($data, TRUE));