Welcome Guest, Not a member yet? Register   Sign In
Update function does not work - Beginner problem
#1

[eluser]kristo5747[/eluser]
Hello,

I am a newbie with PHP and CodeIgniter. I am building a CRUD app that works well expect for the update operation.

My controller:
Code:
<?php

class Site extends CI_Controller {

       function __construct()
       {
            parent::__construct();
        $this->is_logged_in();
       }

    function members_area()
    {
        $data = array();
        
        if($query = $this->site_model->get_records()) // site model is autoloaded.
        {
            $data['records'] = $query;
        }
        
        $this->load->view('members_area', $data);
    }

    function create()
    {
        $data = array(
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content')
        );
        
        $this->site_model->add_record($data);
        $this->members_area();
    }

    function delete()
    {
        $this->site_model->delete_row();
        $this->members_area();
    }

    function update()
    {
        $id = $this->uri->segment(3);
        $data = array(
            'title' => $this->input->post('updtitle'),
            'content' => $this->input->post('updcontent')
        );
        
        $this->site_model->update_record($id, $data);
        $this->members_area();
    }

    function is_logged_in()
    {
    $is_logged_in = $this->session->userdata('is_logged_in');

    if(!isset($is_logged_in) || $is_logged_in != true)
        {
                $this->load->view('login_form');
        }
    }
}

My model:
Code:
<?php

class Site_model extends CI_Model {
    
    function get_records()
    {
        $query = $this->db->get('data');
        return $query->result();
    }

    function add_record($data)
    {
        $this->db->insert('data', $data);
        return;
    }

    function delete_row()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');
    }

     function update_record($id, $data)
    {
        $this->db->where('id', $id);
        $this->db->update('data', $data);
    }

My view:
Code:
<h2>update</h2>
    &lt;?php echo form_open('site/update');?&gt;
    <p>
        <label for="update title">Update Title:</label>
        &lt;input type="text" name="updtitle" id="updtitle" /&gt;
    </p>
    
    <p>
        <label for="update content">Update Content:</label>
        &lt;input type="text" name="updcontent" id="updcontent" /&gt;
    </p>    
    
    <p>
        &lt;input type="submit" value="Update" /&gt;
    </p>
    &lt;?php echo form_close(); ?&gt;
    
    <hr />
    

&lt;/body&gt;
&lt;/html&gt;

If I attempt to update a record in my db, the update does NOT happen. No error message pops up.

What am I missing?
#2

[eluser]Wondering Coder[/eluser]
in your model you didn't set your $id
Code:
function update_record($data)
    {
        $this->db->where('id', $id); //you didn't declare this $id
        $this->db->update('data', $data);
    }
should be
Code:
function update_record($data,$id)
    {
        $this->db->where('id', $id); //you didn't declare this $id
        $this->db->update('data', $data);
    }

or

function update_record($data)
    {
        $this->db->where('id', 1); //this will update your table data with id =1
        $this->db->update('data', $data);
    }

and of course you have to change your controller also
should be like this: $this->site_model->update_record($data, $id);
#3

[eluser]kristo5747[/eluser]
Okay...I am a dunce.

I updated my controller like so
Quote: function update() // mine too
{

$id = $this->uri->segment(3);
$data = array(
'title' => $this->input->post('updtitle'),
'content' => $this->input->post('updcontent')
);

$this->site_model->update_record($data, $id);
$this->members_area();
}

and my model like so

Quote: function update_record($data,$id)
{
$this->db->where('id', $id);
$this->db->update('data', $data);
}

No errors. No update either. What the @!#$ am I missing??
#4

[eluser]InsiteFX[/eluser]
Maybe you need to add the form_validation to site/update?

Also when using for="" the name should match id="" and the names should be the same!
Code:
<label for="updtitle">Update Title:</label>
&lt;input type="text" name="updtitle" id="updtitle" /&gt;

InsiteFX
#5

[eluser]Sudz[/eluser]
Check Whether you are getting value of $id and $data
in your function update_record($data,$id) {....}
#6

[eluser]InsiteFX[/eluser]
Try eching your values in your controller/update method!
Code:
echo $this->uri->segment(3);
echo $id;

InsiteFX
#7

[eluser]kristo5747[/eluser]
Dear all,

echo-ing $id and $data helped. Nothing was being output therefore nothing was being captured. Doing more reading got to redesign my app.

Now, I rely on a URI on the record I want to update:

1) in my controller, I added this update function
Code:
function update() {
    $id = $this->uri->segment(3);
    $title = $this->input->post('title');
    $content = $this->input->post('content');

    $this->posts_model->update_record($id, $title, $content)
}

2) in my model, I added this function
Code:
function update_record($id, $title, $content) {
    $data = array(
        'title' => $title,
        'content' => $content
    );

    $this->db->where('id', $id);
    $this->db->update('data', $data);
}

3) finally, I created a view just for the update
Code:
&lt;?php $this->load->helper('form'); ?&gt;

&lt;?php echo form_open('site/update/'.$id); ?&gt;

        &lt;?php echo form_input('title'); ?&gt;
        &lt;?php echo form_textarea('content'); ?&gt;
        &lt;?php echo form_submit('submit', 'Submit'); ?&gt;

&lt;?php echo form_close(); ?

It works fine now. All I have to do is point my browser to site/update/post-id_number.

What I need to figure out is how to tailor the interface so I can get merge the update view with the other one I have.

Thanks to all of you for taking the time.

Al.




Theme © iAndrew 2016 - Forum software by © MyBB