Welcome Guest, Not a member yet? Register   Sign In
Help required with update and insert
#1

[eluser]the_unforgiven[/eluser]
Hi All,

I'm still learning CI and it's proving to be excellent but I'm now struggling with update and insert, I did have the insert working but now i have added an update function everythings going wrong now, so here's my code.

Controller - admin.php (I do have other things in this controller but these are the two parts am talking about)
Code:
<?php
// Pages Function
    function pages() {

        $data = array();
        
        if($query = $this->page_model->getAllPages())
        {
            $data['pages'] = $query;
            $data['title'] = 'Page Management';
        }
        
        $this->load->view('admin/pages', $data);

    }
    //Update Function
    function update($data) {
        $id = $this->uri->segment(3);
          // Validation Rules
        $this->form_validation->set_rules('name', 'Page Title', 'trim|required|min_length[2]|max_length[120]|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|required|min_length[3]|max_length[160]|xss_clean');
        $this->form_validation->set_rules('keywords', 'Keywords', 'trim|required|min_length[3]|max_length[160]|xss_clean');
        $this->form_validation->set_rules('content', 'Content', 'trim|required|min_length[3]|max_length[2000]|xss_clean');
        
        $data = array(
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'description' => $this->input->post('description'),
            'keywords' => $this->input->post('keywords'),
            'content' => $this->input->post('content'),
            'status' => $this->input->post('status')                

        );
        
        // VALIDATION IS FALSE SHOW FORM
        if ($this->form_validation->run() == FALSE)
        {
            
            foreach ($_POST as $key => $val)
                    $data[$key] = $val;
            
            //$id = $this->uri->segment(3);
            $data['title'] = 'Edit Page';
            $this->load->view('admin/editpage', $data);
        }
        //ELSE INSERT INTO DB
        else {    
            //$id = $this->uri->segment(3);
            $this->page_model->update_page($data);
            redirect('admin/pagesuccess', $data);
        }
?>

Model - page_model.php
Code:
<?php

class Page_model extends CI_Model {
    
    // Get all pages from database
    function getAllPages() {

        $qs = "SELECT * FROM ci_pages ORDER BY id";
        $query = $this->db->query($qs);
        if($query->num_rows()>0){
            return $query->result_array();
        }
    }
    // Create page model
    function add_page($data)
    {
        $this->db->insert('ci_pages', $data);
        return;
    }
    //Update page model
    function update_page($data)
    {
        //$id = $this->input->get('id');
        $this->db->where('id', $this->input->post('id'));
        $this->db->update('ci_pages', $data);
    }
    // Delete Page Model
    function delete_page()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('ci_pages');
    }
}    
?>

View - admin/editpage.php
Code:
<?php include ('header.php'); ?>
<h2>&lt;?php echo $title?&gt;</h2>
&lt;?php

// Show Errors
echo validation_errors('<div class="error">', '</div>');
// Get ID
$id = $this->uri->segment(3);



echo form_open('admin/update/'.$id); ?&gt;
<table name="forms" cellpadding="1">
    <tr>
        <td class="table_title">Page Title:</td>
        <td>&lt;input type="text" name="name" value="" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title">Description:</td>
        <td>&lt;textarea name="description" cols="50" rows="8"&gt;&lt;/textarea></td>
    </tr>
    <tr>
        <td class="table_title">Keywords:</td>
        <td>&lt;input type="text" name="keywords" value="" /&gt;&lt;/td>
        <td><small>max 5 keywords seperated by a comma ,</small></td>
    </tr>
    <tr>
        <td class="table_title">Content:</td>
        <td>&lt;textarea name="content" cols="50" rows="8"&gt;&lt;/textarea></td>
        <td><small>max 2000 characters</small></td>
    </tr>

    <tr>
        <td class="table_title">Status:</td>
        <td><select name="status">
        <option value="active">Active</option>
        <option value="inactive">Inactive</option>
        </select>
        </td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td>&lt;input type="hidden" name="lang_id" value="1" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td>&lt;input type="submit" name="submit" value="Save" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td><a href="admin/pages" title="Back">Back</a></td>
    </tr>
</table>
&lt;?php
echo form_close();
?&gt;

&lt;?php include ('footer.php'); ?&gt;

So in the view file which is an edit form, i want to get the data already in the db to then be able to edit it and when submit is pressed submit that NEW data to the db overwriting the previous data.
#2

[eluser]LuckyFella73[/eluser]
In your model:
Code:
// ..

//Update page model
    function update_page($data)
    {
        //$id = $this->input->post('id');
        $this->db->where('id', $this->input->get('id')); // 'post' instead of 'get'
        $this->db->update('ci_pages', $data);
    }

Check if this is correct:
Code:
// controller
'description' => $this->input->get('description') // get?
#3

[eluser]the_unforgiven[/eluser]
Change both to post but the update function doesn't edit the data in the db nor does it show the fields been filled out with data from db, please help me
#4

[eluser]LuckyFella73[/eluser]
Looking at your view file and controller file I wonder
what you are updating? You first have to send some data
to the form (from your db). Add an hidden field with
name='id' and set the value like your row id from db
(you need that to refer to the db row you are editing).

Your model does not know which row to update.
#5

[eluser]the_unforgiven[/eluser]
Right ok so if i did the following:

Code:
&lt;input type="hidden" name="id" value=""&gt;

Would that the work?
#6

[eluser]the_unforgiven[/eluser]
By adding in
Code:
&lt;input type="hidden" name="id" value="&lt;?php echo $id ?&gt;" /&gt;
that update worked!!

Thanks LuckyFella73, no my question is how doo i get the data already in the db to populate in the form like in standard php you would just echo the varible in the value can this be done in CI?
#7

[eluser]LuckyFella73[/eluser]
You could do it like in the code provided. It's code from
an older project but basically it works like that. I deleted as many
rows as possible to make it easier to read for you. Hope it
helps you to set up your controller:

Code:
// method 'edit' in your controller:
function edit()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    
    if ( isset($_POST['form']) AND $_POST['form'] == 'edit' ) // hidden input field with name='form' and value='edit'
    {
        # Form was send:
        $this->id = $this->input->post('row_id');

        $this->load->helper('form');
        $this->load->library('form_validation');

        # Rules:
        $this->form_validation->set_rules('title', 'Title', 'trim|required');
        $this->form_validation->set_rules('text', 'Text', 'trim|required');



        if ($this->form_validation->run() == FALSE)
        {
        
            $data['row_id'] = $this->input->post('row_id');

            $data['title'] = $this->input->post('title');
            $data['text'] = $this->input->post('text');

            $this->load->view('view_edit', $data);
        }
        else
        {                
            $data_db = array(
                'title' => $this->input->post('title'),
                'text' => $this->input->post('text')
            );
            
            $query = $this->YOUR_model->update($data_db, $this->id);

            if ( $query === FALSE)
            {
                $data['msg'] .= 'failed'; // echo in view file
            }
            redirect('list_entries', 'refresh');
        }
    }
    else
    {
        $this->id = $this->uri->segment(3);
        $data['row_id'] = $this->id;
        
        # Get DB data
        $query = $this->YOUR_model->get_row($this->id);

        if ($query === FALSE)
        {
            echo ("query failed");
            exit();
        }
        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $data['row_id'] = $this->id;
                $data['title'] = $row->title;
                $data['text'] = $row->text;
            }
        }
        else
        {
            echo("Fehler beim Abrufen der zu editierenden Daten aus der Datenbank (DE)");
            exit();
        }
        // send db data to the form
        $this->load->view('view_edit', $data);
    }
}

// view file components:
&lt;input type="hidden" id="row_id" name="row_id" value="&lt;?php echo set_value('row_id', $row_id); ?&gt;" /&gt;
&lt;input type="text" name="title" id="title" value="&lt;?php echo set_value('title', $title); ?&gt;"/&gt;
&lt;textarea name="text" id="textarea"&gt;&lt;?php echo set_value('text', $text); ?&gt;&lt;/textarea&gt;
#8

[eluser]the_unforgiven[/eluser]
Cheers i'll give the a whirl and see what happens, and will update this post shortly.

Thanks again dude Smile
#9

[eluser]the_unforgiven[/eluser]
Still no joy copied everything you said and changed accordingly

Code:
function update() {
            $this->load->helper('form');
            $this->load->library('form_validation');
            
            if ( isset($_POST['form']) AND $_POST['form'] == 'edit' ) // hidden input field with name='form' and value='edit'
            {
                # Form was send:
                $this->id = $this->input->post('id');

                $this->load->helper('form');
                $this->load->library('form_validation');

                # Rules:
                $this->form_validation->set_rules('name', 'Page Title', 'trim|required|min_length[2]|max_length[120]|xss_clean');
                $this->form_validation->set_rules('description', 'Description', 'trim|required|min_length[3]|max_length[160]|xss_clean');
                $this->form_validation->set_rules('keywords', 'Keywords', 'trim|required|min_length[3]|max_length[160]|xss_clean');
                $this->form_validation->set_rules('content', 'Content', 'trim|required|min_length[3]|max_length[2000]|xss_clean');



                if ($this->form_validation->run() == FALSE)
                {

                    $data['id'] = $this->input->post('id');
                    $data['name'] = $this->input->post('name');
                    $data['description'] = $this->input->post('description');
                    $data['keywords'] = $this->input->post('keywords');
                    $data['content'] = $this->input->post('content');
                    $data['status'] = $this->input->post('status');

                    $this->load->view('admin/editpage', $data);
                }
                else
                {                
                    $data = array(
                        'name' => $this->input->post('name'),
                        'description' => $this->input->post('description'),
                        'keywords' => $this->input->post('keywords'),
                        'content' => $this->input->post('content'),
                        'status' => $this->input->post('status')                

                    );

                    $query = $this->page_model->update_page($data, $this->id);

                    if ( $query === FALSE)
                    {
                        $data['msg'] .= 'failed'; // echo in view file
                    }
                    redirect('admin/editpage', 'refresh');
                }
            }
            else
            {
                $this->id = $this->uri->segment(3);
                $data['id'] = $this->id;

                # Get DB data
                $query = $this->page_model->getAllPages($this->id);

                if ($query === FALSE)
                {
                    echo ("query failed");
                    exit();
                }
                if ($query->num_rows() > 0)
                {
                    foreach ($query->result() as $row)
                    {
                        $data['id'] = $row->id;
                        $data['name'] = $row->name;
                        $data['description'] = $row->description;
                        $data['keywords'] = $row->keywords;
                        $data['content'] = $row->content;
                        $data['status'] = $row->status;
                    }
                }
                else
                {
                    echo("Fehler beim Abrufen der zu editierenden Daten aus der Datenbank (DE)");
                    exit();
                }
                // send db data to the form
                $this->load->view('admin/editpage', $data);
            }
        
        
    
        
    }

My view file for edit form
Code:
&lt;?php include ('header.php'); ?&gt;
<h2>&lt;?php echo $title?&gt;</h2>
&lt;?php

// Show Errors
echo validation_errors('<div class="error">', '</div>');
// Get ID
$id = $this->uri->segment(3);

echo form_open('admin/update/'.$id); ?&gt;
<table name="forms" cellpadding="1">
    <tr>
        <td class="table_title">Page Title:</td>
        <td>&lt;input type="text" name="name" value="" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title">Description:</td>
        <td>&lt;textarea name="description" cols="50" rows="8"&gt;&lt;/textarea></td>
    </tr>
    <tr>
        <td class="table_title">Keywords:</td>
        <td>&lt;input type="text" name="keywords" value="" /&gt;&lt;/td>
        <td><small>max 5 keywords seperated by a comma ,</small></td>
    </tr>
    <tr>
        <td class="table_title">Content:</td>
        <td>&lt;textarea name="content" cols="50" rows="8"&gt;&lt;/textarea></td>
        <td><small>max 2000 characters</small></td>
    </tr>

    <tr>
        <td class="table_title">Status:</td>
        <td><select name="status">
        <option value="active">Active</option>
        <option value="inactive">Inactive</option>
        </select>
        </td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td>&lt;input type="hidden" name="lang_id" value="1" /&gt;&lt;/td>
        <td>&lt;input type="hidden" name="id" value="&lt;?php echo $id ?&gt;" /&gt;&lt;/td>
        <td>&lt;input type="hidden" name="form" value="edit" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td>&lt;input type="submit" name="submit" value="Save" /&gt;&lt;/td>
    </tr>
    <tr>
        <td class="table_title"></td>
        <td><a href="admin/pages" title="Back">Back</a></td>
    </tr>
</table>
&lt;?php
echo form_close();
?&gt;

&lt;?php include ('footer.php'); ?&gt;
#10

[eluser]LuckyFella73[/eluser]
Would be good to know what exactly doesn't work.
What I see is that you didn't extend all input fields
you have in your form.

Notice what I posted regarding the view file:
Code:
// view file components:
&lt;input type="hidden" id="row_id" name="row_id" value="&lt;?php echo set_value('row_id', $row_id); ?&gt;" /&gt;
&lt;input type="text" name="title" id="title" value="&lt;?php echo set_value('title', $title); ?&gt;"/&gt;
&lt;textarea name="text" id="textarea"&gt;&lt;?php echo set_value('text', $text); ?&gt;&lt;/textarea&gt;

You input fields have to follow the same 'style' (set_value ...) otherwise your form doesn't repopulate if the validation fails. Look at the user guide to get it
working with your select/option part.

Btw.: My code was not meant to work for you "out of the box". I just wanted to
show the basic principle you could handle db updates!

If you post what exactly doesn't work I'll try to help you.




Theme © iAndrew 2016 - Forum software by © MyBB