Forms Codeigniter-使用复选框从表单更新表

Forms Codeigniter-使用复选框从表单更新表,forms,codeigniter,checkbox,updates,multiple-records,Forms,Codeigniter,Checkbox,Updates,Multiple Records,我正在尝试使用Codeigniter更新MySQL表 我的型号代码是: function update_customer_records($updatedrow) { $this->db->where('id',$this->input->post('id')); $this->db->update('customers',$updatedrow); } 我的看法是: $attributes=array( 'name'

我正在尝试使用
Codeigniter
更新
MySQL

我的型号代码是:

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(); ?>
我有两个问题:

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 );
    }
}
function update_customer_records( $id, $data )
{
    $this->db->where( 'id', $id );
    $this->db->update( 'customers', $data );
}
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');
}
  • 如果未选中复选框,如何停止foreach的运行
  • 如何将数组正确地传递给模型,以便运行更新
  • 一如既往地提前感谢。

    是否与重复主题

    如果未选中复选框,如何停止foreach的运行

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

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

    你的模型错了。应该是:

    public function update_customer_records($updatedrow)
    {
        $this->db->update('customers', $updatedrow);
    }
    
    您不需要编写
    $this->db->where('id',$this->input->post('id')
    (这是错误的,因为您不能在模型中使用$this->input->post()),因为您已经在
    $updaterow
    中传递了
    id

    对不起,我读你的代码太快了

    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')));
    }
    

    首先,我发现表单中有两个字段具有相同的名称和id(如下所示),在一个字段中设置其值
    customer\u 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>
    
    如果未找到或未随表单一起提交
    editcustomer
    ,则
    if
    条件将返回false。此外,表单中没有
    id
    字段,在这种情况下,您不能使用
    $this->input->post('id')
    ,因此如果您需要在模型的
    where子句中选中复选框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 );
        }
    }
    
    function update_customer_records( $id, $data )
    {
        $this->db->where( 'id', $id );
        $this->db->update( 'customers', $data );
    }
    
    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');
    }
    
    我认为您不需要传递
    'id'=>$row,
    ,因为您可能不想更新此字段。此外,在更新记录之前,还应使用检查表单输入(您可以设置绑定用户以输入邮政编码所需的邮政编码字段)

    在模型中:

    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 );
        }
    }
    
    function update_customer_records( $id, $data )
    {
        $this->db->where( 'id', $id );
        $this->db->update( 'customers', $data );
    }
    
    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');
    }
    
    所以它将执行类似的操作(伪代码)

    更新: 我想你也可以用这个

    在控制器中:

    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 );
        }
    }
    
    function update_customer_records( $id, $data )
    {
        $this->db->where( 'id', $id );
        $this->db->update( 'customers', $data );
    }
    
    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');
    }
    
    在模型中:

    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 );
        }
    }
    
    function update_customer_records( $id, $data )
    {
        $this->db->where( 'id', $id );
        $this->db->update( 'customers', $data );
    }
    
    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');
    }
    

    你能告诉我为什么
    OP
    不能在模型中使用
    $this->input->post('id')
    吗?这在技术上是可能的,但不是最佳实践。从控制器访问$this->input->post()更灵活,更符合MVC设计模式(在我看来)。我这样问是因为你写了
    你不能使用
    :-)我的错!我应该解释为什么不直接在模型中使用$this->input->post()更好,而不是仅仅编写它是不可能的。PS:对不起,我的英语很差!您好,谢赫,非常感谢您的解释。@Smudger,很高兴知道:-)