Welcome Guest, Not a member yet? Register   Sign In
CI nob - Update records
#1

[eluser]Mr. Gibon[/eluser]
Hi!

I'm developing site, and have troubles with records update, in ci.

this is my code:
model
Code:
class Shop_model extends Model {
     function get_records()
    {
        $query = $this->db->get('table');
        return $query->result();
    }
    function add_record($data)
    {
        $this->db->insert('table',$data);
        return;    
    }
    function update_record($data)
    {
        $this->db->where('id', $id);
        $this->db->update('table',$data);
        return;
    }
    function delete_row()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('table');
    }
}

controller:
Code:
class Shop extends Controller {
     function index()
    {
        $data = array();
        if($query = $this->shop_model->get_records())
            {
                $data['records'] = $query;
            }
        $data['title']  = 'Szop!';
        $this->parser->parse('shop_view', $data);

    }
    
    function create()
    {
        $data = array(
            'nazwa' => $this->input->post('nazwa'),
            'klasa' => $this->input->post('klasa')
        );
        
        $this->shop_model->add_record($data);
        $this->index();
    }
    
    function update()
    {
        $data = array(
            'id' => $this->input->post('id'),
            'nazwa' => $this->input->post('nazwa'),
            'klasa' => $this->input->post('klasa')    
        );
        
        $this->shop_model->update_record($data);
    }
    
    
    function delete()
    {
        $this->shop_model->delete_row();
        $this->index();
    }
}

and view:

Code:
<h2>Create</h2>
    &lt;?php echo form_open('shop/create');?&gt;
    <p>
        <label for="nazwa">nazwa:</label>
        &lt;input type="text" name="nazwa" id="nazwa" /&gt;
    </p>
    
    <p>
        <label for="klasa">klasa:</label>
    <select name="klasa"><option>1</option><option>2</option><option>3</option>
    (...)</select>
    </p>    
    
    <p>
        &lt;input type="submit" value="Submit" /&gt;
    </p>
    &lt;?php echo form_close(); ?&gt;
    
    <hr />
    
    <h2>update</h2>
    &lt;?php echo form_open('shop/update');?&gt;
    <p><label for="nazwa">nazwa:</label>
        &lt;input type="text" name="nazwa" id="nazwa" /&gt;&lt;/p>

    <p><label for="id">id:</label>
                &lt;input type="text" name="id" id="id" /&gt;&lt;/p>
    <p>
    <label for="klasa">klasa:</label>
    <select name="klasa"><option>1</option><option>2</option><option>3</option>
    (...)</select>
    </p>    
    
    <p>&lt;input type="submit" value="Submit" /&gt;&lt;/p>
    &lt;?php echo form_close(); ?&gt;

    <h2>Read</h2>
    &lt;?php if(isset($records)) : foreach($records as $row) : ?&gt;
    
    <h2>delete: &lt;?php echo anchor("shop/delete/$row->id", $row->nazwa); ?&gt; </h2>
    <div>&lt;?php echo $row->klasa; ?&gt;</div>        
    &lt;?php endforeach; ?&gt;

    &lt;?php else : ?&gt;    
    <h2>No records were returned.</h2>
    &lt;?php endif; ?&gt;

after filling up update form, got error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: models/shop_model.php

Line Number: 21

How to make it cooperate Wink?
Thanks for help!
#2

[eluser]flaky[/eluser]
where are you getting the $id variable?
Code:
function update_record($data)
    {
        $this->db->where('id', $id);
        $this->db->update('table',$data);
        return;
    }

I wouldn't be surprised if the id is here
Code:
$data['id']
#3

[eluser]Mr. Gibon[/eluser]
I do not know Sad

How fix it?
---------------
FIXED! Wink
the right code is here Wink
Code:
function update_record($data)
    {
        $this->db->where('id', $this->input->post('id'));
        $this->db->update('items',$data);
        return;
    }
#4

[eluser]Aidy[/eluser]
Looking at your controller you have saved the id in the array as a key value pair which you can get like

Code:
$data['id']

So you could use it in your update method like this

Code:
function update_record($data)
    {
        $this->db->where('id', $data['id']);
        $this->db->update('table',$data);
        return;
    }

Or perhaps better to save it as a variable and pass it to your method like this.

Contoller

Code:
function update()
    {
        $data = array(
            'nazwa' => $this->input->post('nazwa'),
            'klasa' => $this->input->post('klasa')    
        );

$id = $this->input->post('id');
        
        $this->shop_model->update_record($data, $id);
    }

Model

Code:
function update_record($data, $id)
    {
        $this->db->where('id', $id);
        $this->db->update('table',$data);
        return;
    }
#5

[eluser]Mr. Gibon[/eluser]
great tip, it makes code cleaner !

thanks!




Theme © iAndrew 2016 - Forum software by © MyBB