使用foreach循环更新具有相同ID的多行 [英] Update multiple rows with same id using foreach loop

查看:90
本文介绍了使用foreach循环更新具有相同ID的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为数据库中的每一行(其中id = $ id)回显2个输入字段(2 db列),每行的数据都在输入字段内。管理员用户可以在输入字段中更改数据,然后单击更新。

I echo 2 input-fields (2 db columns) foreach row in the database (where id=$id) with each row's data being inside the input field. Admin-user can change data inside input-field and click update.

当前,当单击更新按钮时,它会更新行(其中id = $ id),但会使用第一个输入的输入数据来更新每一行字段,而不是更新每个输入字段的输入数据。

Currently, when clicking the update-button, it does update the rows (where id=$id) but it updates each row with the input-data of the first input field instead of updating input-data foreach input field. Thank you in advance for help.

view:

<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
    <?php foreach($link_data as $row) : ?>
        Link:<br />
        <input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
        <input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
    <?php endforeach; ?>

        <input type='submit' name='update_site' value='Update'>
</form>

控制器:

public function do_edit_page(){
    $id = $this->input->post('page_id', TRUE);
    $this->content_model->update_links($id);
}

型号:

public function update_links($id){
    foreach($_POST as $update_rows){
        $update_rows = array(
            array(
              'page_id' => $id,
              'link_title' => $this->input->post('page_link_title', TRUE),
              'link' => $this->input->post('page_link_sub_title', TRUE)  
            )
        );
        $this->db->update_batch('content_links', $update_rows, 'page_id');  
    }  
}


推荐答案

you必须在foreach循环之间移动form标记,以便它可以提交单行数据。像这样的

you have to move your form tag between the foreach loop so that it can submit the data of single row. something like this

<?php foreach($link_data as $row) : ?>
  <form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
    Link:<br />
    <input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
    <input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
    <input type='submit' name='update_site' value='Update'>
  </form>
<?php endforeach; ?>

这篇关于使用foreach循环更新具有相同ID的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆