2013-03-02 176 views
1

我试图用Codeigniter更新MySQL表。Codeigniter - 从表单复选框更新表

我的模型代码是:

function update_customer_records($updatedrow) 
{ 
    $this->db->where('id',$this->input->post('id')); 
    $this->db->update('customers',$updatedrow); 
} 

我的看法是:

$attributes=array(
     'name'=>'updatecustomer', 
     'id'=>'updatecustomer', 
     ); 
    echo form_open('masterdata/manage_customers',$attributes); 
?> 

<table> 
    <tr> 
     <td>&nbsp;</td><td>&nbsp;</td><td>Customer Name</td><td>postalcode</td> 
    <tr> 

<?php if(isset($records)) : foreach ($records as $row) : ?> 
    <tr> 
     <td> 
<?php echo anchor('masterdata/customers_updated/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?> 
     </td> 
     <td> 
      <input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>"> 
     </td> 
     <td> 
      <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" > 
     </td> 
     <td> 
      <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" > 
     </td> 
    </tr> 
<?php endforeach ; ?> 
    </table> 
<input type="submit" value="Update Selected"> 
<?php else : ?> 
<h2> No Records Found</h2> 
<?php endif; ?> 
<?php echo form_close(); ?> 

我的控制器:

function manage_customers() 
    { 

     $data['title']="Manage Customers"; 

      //query model to get data results for form 
      $data=array(); 

      if($query=$this->model_master_data->get_records()){ 
       $data['records']=$query; 


      $this->load->view("master_data/view_master_data_header",$data); 
      $this->load->view("master_data/view_master_data_nav"); 
      $this->load->view("master_data/view_content_master_data_manage_customers",$data); 
      $this->load->view("master_data/view_master_data_footer"); 


      $editcustomer = $this->input->post('editcustomer'); 

      if(isset($editcustomer)){ 

       //begin outputting id of selected checkbox 
       foreach ($editcustomer as $row) : 
        echo $row; 

       $updatedrow=array(
        'id'=>$row, 
        'postalcode'=>'44444' 
        ); 


       $this->model_master_data->update_customer_records($updatedrow); 

       endforeach; 
      } 

我有两个问题:

  1. 如果未检查复选框,如何停止运行的foreach。
  2. 如何正确地将数组传递到模型,以便更新运行?

在此先感谢一如既往。

回答

2

首先我发现在具有相同的名称和ID(如下)形式的两个领域,一个领域要设置它的价值customer_name和另一个你正在设置postalcode

<td> 
    <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" > 
           --^^-- 
</td> 
<td> 
    <input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" > 
           --^^-- 
</td> 

所以我认为(可能)的名称和第二场的编号应根据它的值是postalcode

此外,您不必担心foreach循环,因为循环中的代码只有在检查窗体上的复选框时才会运行,因为不会提交未选中的复选框,但您可以检查并运行循环使用下面的代码

if($this->input->post('editcustomer') != false) 
{ 
    foreach ($editcustomer as $row) 
    { 
     // code here 
    } 
} 

如果editcustomer找不到或不与表单提交的if条件将返回false。还没有id领域的形式,在这种情况下,你不能使用$this->input->post('id'),所以如果你需要检查你的模型where clause复选框ID,那么你可以使用

在控制器:

if($this->input->post('editcustomer') != false) 
{ 
    $this->load->model('model_master_data'); 
    foreach ($editcustomer as $row_id) 
    { 
     $data = array('postalcode' => '44444'); 
     $this->model_master_data->update_customer_records($row_id, $data); 
    } 
} 

我不认为你需要通过'id'=>$row,,因为你可能不wan't更新这一领域。在更新记录之前,您还应该使用form validation来检查表单输入(您可以设置绑定用户输入邮政编码所需的邮编字段)。

在模型:

function update_customer_records($id, $data) 
{ 
    $this->db->where('id', $id); 
    $this->db->update('customers', $data); 
} 

所以它会做这样的事情(伪代码)

update the customers table set `postalcode` = '44444' where `id` = $id 

更新: 我想你也可以使用update_batch

在控制器:

if($this->input->post('editcustomer') != false) 
{ 
    $data = array(); 
    foreach ($editcustomer as $row_id) 
    { 
     $data[] = array('id' => $row_id, 'postalcode' => '44444'; 
    } 
    $this->load->model('model_master_data'); 
    $this->model_master_data->update_customer_records($data); 
} 

在模型:

function update_customer_records($data) 
{ 
    $this->db->update_batch('customers', $data, 'id'); 
} 
+0

嗨谢克,真的很感谢你的解释。 – Smudger 2013-03-03 07:14:21

+0

@Smudger,很高兴知道:-) – 2013-03-03 07:31:43

1

"Codeigniter update mysql table from form with CI"重复的话题?

如果未检查复选框,如何停止运行的foreach。

如果未选中复选框,则不必停止运行的foreach。 因为$editcustomer变量只包含复选框。

如何正确地将数组传递到模型,以便更新运行?

您的模型是错误的。它应该是:

public function update_customer_records($updatedrow) 
{ 
    $this->db->update('customers', $updatedrow); 
} 

你不需要写$this->db->where('id',$this->input->post('id'));(它是错的,因为你不能在模型中使用$这个 - >输入 - >后()),因为你已经在通过id$updaterow

编辑:我很抱歉,我太快读了你的代码!

function update_customer_records($updatedrow) 
{ 
    $this->db->where('id',$this->input->post('id')); 
    $this->db->update('customers',$updatedrow); 
} 

...是正确的。或者,您可以在更短的方式把它写:

function update_customer_records($updatedrow) 
{ 
    $this->db->update('customers', $updatedrow, array('id' => $this->input->post('id'))); 
} 
+0

你能告诉为什么'OP'不能使用'$这个 - >输入 - >在模型中发布('id')'? – 2013-03-02 23:25:55

+1

这是(技术)可能的,但它不是一个最佳实践。从控制器访问$ this-> input-> post()更灵活,并且更符合MVC设计模式(在我看来)。 – Guicara 2013-03-03 00:03:21

+0

我问你是因为你写了'你不能用':-) – 2013-03-03 00:07:04