0

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.

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>

controller:

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

model:

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');  
    }  
}
1
  • where from you are getting $id = $this->input->post('page_id', TRUE); in controller. Commented Sep 24, 2013 at 10:42

1 Answer 1

0

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; ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.