Welcome Guest, Not a member yet? Register   Sign In
non-object
#1

[eluser]GamingFusion[/eluser]
When trying to get information from a line in my database table and display it in either a text for or text area I get this error

Quote:Trying to get property of non-object

Code:

controller
Code:
function editShow()
    {
        $data['title'] = 'Theater 311 | Edit Show';
        $data['query'] = $this->database->getShow($this->uri->segment(3));
        
        $this->load->view('editShow', $data);
    }

model
Code:
//getShow Function
    function getShow($id)
    {
        //Get Shows Data
        $this->db->where('id', $id);
        $query = $this->db->get('shows');
        
        return $query->result();
    }

view
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  
&lt;?=form_open('theater/updateShow');?&gt;

<table>
    <tr>
        <td>Show Title: </td><td>&lt;input name="title" class="input" type="text" value="&lt;?=$query-&gt;title?&gt;" /></td>
    </tr>
    <tr>
        <td>Show Description: </td><td>&lt;textarea name="desc" class="input" cols="30" rows="5"&gt;&lt;?=$query->desc?&gt;&lt;/textarea&gt;&lt;/td>
    </tr>
    <tr>
        <td>Show Times: </td><td>&lt;textarea name="times" class="input" cols="30" rows="5"&gt;&lt;?=$query->showtimes?&gt;&lt;/textarea&gt;&lt;/td>
    </tr>
    <tr>
        <td>Cast: </td><td>&lt;textarea name="cast" class="input" cols="30" rows="5"&gt;&lt;?=$query->cast?&gt;&lt;/textarea&gt;&lt;/td>
    </tr>
    <tr>
        <td>Director: </td><td>&lt;input name="director" class="input" type="text" value="&lt;?=$query-&gt;director?&gt;" /></td>
    </tr>
    <tr>
        <td>&lt;?=form_submit('submit', 'Add Show');?&gt;</td><td>&lt;?=form_reset('reset', 'reset');?&gt;</td>
    </tr>
</table>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Jamie Rumbelow[/eluser]
Hi GamingFusion,

The mistake you've made is in the Model - you're calling $query->result(), which returns an array instead of $query->row(), which returns an object. You could either change this in the model or you could access the first object in the array with the '0' index. I'd change the model, so the model function should look like so:

Code:
function getShow($id)
    {
        //Get Shows Data
        $this->db->where('id', $id);
        $query = $this->db->get('shows');
        
        return $query->row();
    }
#3

[eluser]jedd[/eluser]
Where are you testing if id is valid and present?

Where are you testing if there are rows in the db that match that id?

Change your controller method to:
Code:
function editShow( $id )
    {
        $data['title'] = 'Theater 311 | Edit Show';
        if ($id)
            $data['query'] = $this->database->getShow($id);
. . .

Use some echos / var_dumps to see whether the DB call is succeeding.
#4

[eluser]GamingFusion[/eluser]
Thanks Jamie it work.

jedd the id has to be valid because you clikc a link on a other page that gets all the shows in the database with a valid id
#5

[eluser]jedd[/eluser]
[quote author="GamingFusion" date="1256533598"]
jedd the id has to be valid because you clikc a link on a other page that gets all the shows in the database with a valid id
[/quote]

Allow me to disabuse you of this belief.

If you're taking the input from your URL - as per your code $this->uri->segment(3) - there is no 'has to be valid' about it. A user may modify the URL to be any value they choose, or you may have some logic bug elsewhere (perhaps where you didn't check your data) ... regardless of what you want to believe about the nature of the URL.

You should take the value in as a parameter to your function, you should have a default value, you should check/sanitise the data, and you should check for the case of no rows being returned.




Theme © iAndrew 2016 - Forum software by © MyBB