更新两个使用单个查询

时间:2016-11-11 06:29:54

标签: mysql codeigniter-3

这是我的两个表,即bill和product_list。

product_list表看起来像这样

id    bill_id       description               quantity          amount

19      18      ["item1","item2","item3"]   ["2","1","2"]   ["100","100","150"]
账单表看起来像这样..

id  invoice_no      pp_no       

18  JED00018         34533  

控制器看起来像这样..

    function edit_bill($id)
    {
    var_dump($id);
    $this->form_validation->set_rules('pp_no',Number','required');
    if($this->form_validation->run()==true)
    {
        $data=array('pp_no'=>$this->input->post('pp_no'));
        if($this->Bills_model->bill_update($id,$data))
        {
            $this->session->set_flashdata('success_msg','Bills Updated successfully.');
            redirect('bills.html');

        }
    }
    $data['result']=$this->Bills_model->view_bill_by_id($id);
    $data['search_url'] = 'bills.html';
    $data['active'] = 'bill';
    $data['module'] = 'admin/edit_bill';
    $this->load->view('admin/master',$data);

}

模型看起来像

function bill_update($id,$data)
{

        $this->db->join('product_list','product_list.bill_id=bills.id');
        $this->db->where('bills.id',$id);
        $this->db->update('bills',$data);

        $data['description']=$this->input->post('description',true);
        $description=json_encode($data['description']);
        $data['quantity']=$this->input->post('quantity',true);
        $quantity=json_encode($data['quantity']);
        $amount=json_encode($this->input->post('amount',true));
        $data2=array('description'=>$description,'quantity'=>$quantity,'amount'=>$amount);
        $this->db->join('bills','bills.id=product_list.bill_id');
        $this->db->where('product_list.bill_id',$id);
        $this->db->update('product_list',$data2);

       if($this->db->affected_rows() != 0 )
        {
            return TRUE;
        }  
        else
       {
           return FALSE;
       }
 }

我的观看表单如下

  <form class="form-horizontal" method="post" action="<?php echo base_url(); ?>bills/edit_bill/<?php echo $result->id; ?>">
  <div class="clearfix"></div>
  <div class="form-group">

     <label for="pp_no" class="col-sm-2 control-label">PP NO/ID</label>
     <?php echo form_error('pp_no'); ?>
     <div class="col-sm-3" style="padding-top:5px;">
     <input type="text" class="form-control"  name="pp_no" id="pp_no" value="<?php echo $result->pp_no;?>" >
    <input type="hidden" name="id" value="<?php echo $result->id;?>">
      </div>
     </div>

      <table id="items">

       <tr>
          <th>Item No</th>
          <th>Description</th>
          <th>Amount</th>
          <th>Quantity</th>
          <th>Total</th>
       </tr>

         <?php $description=json_decode($result->description)?>
         <?php $amount=json_decode($result->amount)?>
         <?php $quantity=json_decode($result->quantity);?>


    <?php foreach($description as $index => $row){
           ?>

  <tr class="item-row">

      <td class="item-no"><div class="delete-wpr"><textarea class="textarea"></textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td>
      <td class="description"><textarea class="textarea" name="description[]"><?php echo $row;?></textarea></td>
      <td><textarea name="amount[]" class="cost textarea"><?php echo $amount[$index];?></textarea></td>
      <td><textarea name="quantity[]" class="qty textarea"> <?php echo $quantity[$index];?></textarea></td>
      <td><span class="price"></span></td>
  </tr>
  <?php }?>
 </table>
   <div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
  <input type="hidden" name="id" value="<?php echo $result->id;?>">

  <button type="submit" class="btn btn-primary" style="margin-top: 20px;" >Save</button>
 </div>
 </div>
 </form>

这时当我更新任何字段并给出提交时,它会转到像localhost/edit_bill/19这样的product_list的id,并且不知道为什么会这样。

1 个答案:

答案 0 :(得分:0)

it's going to the id of product_list like this localhost/edit_bill/19 and don't have any idea why it's going like this.

Because you are telling it to!

<form class="form-horizontal" method="post" action="<?php echo base_url(); ?>bills/edit_bill/<?php echo $result->id; ?>">

Just loose the bit where you are adding the id to the URL! So it becomes...

<form class="form-horizontal" method="post" action="<?php echo base_url();?>bills/edit_bill">

You are already "posting" the id in your form...

<input type="hidden" name="id" value="<?php echo $result->id;?>">