Undefined variable: data and You must use the "set" method to update an entry.

#1
[eluser]Michal1[/eluser]
Hi guys,

I am totally new to the codeigniter, so at first I would like to apologise if my question will be kinda stupid.

I have been following nice sets of tutorials, mentioned on nettuts.com. Concretely the crud tutorial. So I tried to make an exact same code as the guy in that tutorial, however it does not work.

What a script should do is, insert some data into a table. There are two forms, title and content and one button submitt. But when I hit the button submit I get this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/site_model.php

Line Number: 18

A Database Error Occurred

You must use the "set" method to update an entry.


And I really do not why is that. I looked at the line where a mistake should be, was thinking and tried to change the line:

Code:
function update_record()

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

for:

Code:
function update_record()

    {
      
       $this->db->set('data', $data);
    }

But it still does not work.

My whole model looks like this:

Code:
<?php


class Site_model extends Model {

    function get_records()

    {
        $query = $this->db->get('data');
        return $query->result();
        

    }

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

    function update_record()

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

    function delete_row()

    {

        $this->db->where('id', $this->url->segment(3));
        $this->db->delete('data');
    }

}

Does anybody have any input? thank you very much

#2
[eluser]Cristian Gilè[/eluser]
Hi Michal1,

how do you pass $data to the update function? You need to change your code from

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

to

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

Change other functions accordingly.

Cristian Gilè

#3
[eluser]Michal1[/eluser]
Ah I see. Thank you very much. I thought that because I have:

Code:
$this->site_model->add_record($data);
in my create function, in controller that is enough.

Could you please explain why it is not?

Thank you

M

#4
[eluser]Cristian Gilè[/eluser]
This is a basic programming question related to PHP not CI.

Cristian Gilè


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.