Codeigniter从带有复选框的表单更新mysql表数据

时间:2022-10-06 12:37:59

New to codeigniter and trying to get my head around updating checked rows from a user form.

codeigniter的新手,并尝试从用户表单更新已检查的行。

My view generates a form with MySQL data as below:

我的视图生成一个包含MySQL数据的表单,如下所示:

<?php 
    echo form_open('masterdata/update_customers');
?>

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

<?php if(isset($records)) : foreach ($records as $row) : ?>
    <tr>
        <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="postalcode_<?php echo $row->id ?>" id="postalcode_<?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(); ?>

This works perfectly well as I get my unique name and values for all input fields.

这非常有效,因为我获得了所有输入字段的唯一名称和值。

My issue is now trying to pass the selected checkboxes and there values to codeigniter and get it to update each row.

我的问题是现在尝试将选中的复选框和值发送到codeigniter并让它更新每一行。

traditionally I would use foreach($_POST['editcustomer'] as $editcustomer){ but cant get my head around this in codeigniter.

传统上我会使用foreach($ _ POST ['editcustomer']作为$ editcustomer){但是无法在codeigniter中解决这个问题。

my controller function, update_customers at this stage is very basic:

我的控制器功能,在这个阶段update_customers是非常基本的:

function update_customers()
    {

        $this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
        $this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[6]");


        if ($this->form_validation->run() == FALSE){
            $data["message"]="";

            $data['title']="Master Data Home Page";
            $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");
            $this->load->view("master_data/view_master_data_footer");
        } else {

        $data = array(
            'customer_name' =>  $this->input->post('customer_name'),
            'postalcode' =>  $this->input->post('postalcode'),
            );

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

        $this->customers_updated();
        }
    }

My model function, update_customer_records is:

我的模型函数update_customer_records是:

function update_customer_records($data)
{

        $this->db->where('id',$this->input->post('id'));
    $this->db->update('customers',$data);


}

I know there is quite a bit missing here, like processing only the rows checked. but not sure how to add this in. secondly, my unique name and id's being generated, as per my view is: name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"

我知道这里有相当多的缺失,比如只处理检查的行。但不知道如何添加它。其次,根据我的观点生成我的唯一名称和id:name =“customername _ <?php echo $ row-> id?>”id =“customername _ <?php echo $ row-> id?>“

how do get the update to apply to the unique form input name? I need to append the prefix customername_ and postalcode_ to the input->post(?) value?

如何让更新应用于唯一的表单输入名称?我需要将前缀customername_和postalcode_附加到input-> post(?)值?

Any direction or assistance here will be appreciated. thanks a million in advance.

任何方向或协助将不胜感激。提前感谢一百万。

1 个解决方案

#1


1  

With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.

使用CodeIgniter,如果必须获取复选框值,则必须使用$ this-> input-> post('name_of_your_checkbox')。它返回一个数组。

For example, if you have that in a view:

例如,如果您在视图中有此:

<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />

From the controller:

从控制器:

public function update_customers() {

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

}

Return:

返回:

Array {
    [0] => 1
    [1] => 2
    [2] => 3
}

#1


1  

With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.

使用CodeIgniter,如果必须获取复选框值,则必须使用$ this-> input-> post('name_of_your_checkbox')。它返回一个数组。

For example, if you have that in a view:

例如,如果您在视图中有此:

<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />

From the controller:

从控制器:

public function update_customers() {

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

}

Return:

返回:

Array {
    [0] => 1
    [1] => 2
    [2] => 3
}