使用codeigniter更新多行

时间:2011-05-03 21:29:35

标签: mysql codeigniter

我在使用我的第一个codeigniter项目时遇到了一些麻烦,我觉得答案真的很接近......

我从数据库填充一种形式的输入字段,当用户提交表单时,它应该遍历数据库的行,并根据每行的“id”更新内容。

我的表单(在视图'admin.php'中)看起来像这样:

<?php echo form_open('admin/update'); ?>
<?php foreach($content as $row) : ?>

<?php echo form_input('id['. $row->id .'][id]', $row->id); ?>
<?php echo form_input('id['. $row->id .'][order]', $row->order); ?>
<?php echo form_input('id['. $row->id .'][title_text]', $row->title_text); ?>
<?php echo form_textarea('id['. $row->id .'][body_text]', $row->body_text); ?>
<?php

if ($row->visibility == 'visible') {
    echo form_checkbox('id['. $row->id .'][visibility]', 'visibility', TRUE);
} else {
    echo form_checkbox('id['. $row->id .'][visibility]', 'visibility', FALSE);
}

?>

<?php endforeach;?>
<?php echo form_submit('Save Changes', 'Save Changes'); ?>
<?php echo form_close(); ?>

现在,大部分内容都基于各种教程和帮助文档。这是来自控制器'admin'的代码,当人们点击'保存更改'时,我会调用该代码:

function update()
{
    $this->load->model('admin_model');
    $this->admin_model->updateRecords($_POST['id']);
    $this->index();
}

然后将数据传递给模型,其中函数如下所示:

function updateRecords($data) 
{
    $this->db->insert_batch('content', $data);  
}

这是我以某种方式工作的唯一方法。它可以很好地插入数据,但是根据它们唯一的“id”,将它添加到新行中而不是更新已存在的行。

为了好订单,这里是我在mySQL数据库中的列:

id (PRIMARY and AUTOINCREMENT)
order
title_text
body_text
visibility
origin_date

提前多多谢谢:)

1 个答案:

答案 0 :(得分:1)

这是我如何做到的,我在一些基本的错误处理中添加了;希望这能解释自己:

模型

function updateRecords($records) 
{
    // Method 1
    // If you want to proceed if there is an error
    $errors = array();

    // Method 2
    // If you want to rollback if there is an error
    $this->db->trans_start();

    foreach ($records as $id => $values):

        // Your form fields conveniently match the column names
        // No need to create the data array, we already have it
        // If you want to update `origin_date` just do this:
        // $values['origin_date'] = time();

        // See if checkbox values were sent
        // Assumes visibility is integer
        if (empty($values['visibility']))
        {
            $values['visibility'] = 0;
        }
        else
        {
            $values['visibility'] = 1;
        }

         // Using method 1, store the error id in an array
        $this->db
            ->where('id', $id)
            ->update('content', $values) OR $errors[] = $id;

        // If using method 2, stop the loop
        $this->db
            ->where('id', $id)
            ->update('content', $values) OR break;


    endforeach;

    // Method 1
    // Return the array of failed updates
    // Controller will check if the array is empty
    // You can tell the user which updates failed or just how many
    return $errors;

    // Method 2
    // This will return TRUE or FALSE
    $this->db->trans_complete();
    return $this->db->trans_status();
}

控制器

function update()
{
    $records = $this->input->post('id');
    $this->load->model('admin_model');

    // Make sure to return errors here if they occur

    // Using method 1
    $errors = $this->admin_model->updateRecords($records);
    $num_errors = count($errors);
    if ($num_errors > 0)
    {
        echo "There were {$num_errors} errors!";

        // You can optionally print which records had errors
        echo "There were errors updating these rows: ".implode(', ', $errors);
    }

    // Using method 2
    $success = $this->admin_model->updateRecords($records);
    if ( ! $success)
    {
        echo "Error performing update, no records were saved.";
    }

    $this->index();
}

当然,不要只是回应错误,使用某种消息库,会话数据或向视图发送变量。一些参考文献:

http://codeigniter.com/user_guide/database/transactions.html

http://codeigniter.com/user_guide/database/active_record.html#update