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

[eluser]LuckyFella73[/eluser]
Quote:[17-Aug-2011 13:37:44] PHP Fatal error: Call to a member function num_rows() on a non-object in /Applications/MAMP/htdocs/ci/application/controllers/admin.php on line 106

You get this error because you don't load the database class in your update method.
To solve that load the database class in your contructor or update method.

But that's not the reason you get a white screen.
#42

[eluser]the_unforgiven[/eluser]
Right ok i have put this in and this kinda works

Code:
if ($query === FALSE)
            {
                echo "query failed";
                //exit();
            }
            if(!$query)
            {
                exit($this->db->last->query());
            
            
            if($query->num_rows()>0)
            {
                foreach ($query->result_array() as $row)
                {
                    $data['id'] = $this->id;
                    $data['name'] = $row->name;
                    $data['description'] = $row->description;
                    $data['keywords'] = $row->keywords;
                    $data['content'] = $row->content;
                    $data['status'] = $row->status;
                }
            }
        }

When I say kinda works i then get
Code:
Severity: Notice

Message: Undefined variable: name

Filename: admin/editpage.php

Line Number: 14

Which is what is called in the view file
#43

[eluser]danmontgomery[/eluser]
[quote author="LuckyFella73" date="1313608019"]You get this error because you don't load the database class in your update method.[/quote]

No, if that were the case you'd be getting "Call to a member function get()", or "Call to a member function query()", not num_rows().
#44

[eluser]the_unforgiven[/eluser]
Method - all methods insert and delete wrk fine just the update and get the form populated with the data in the edit form
Code:
<?php

class Page_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->database('ci_pages');
        $this->load->helper(array('form', 'url'));
    }
    
    // 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;
    }

    function update_page()
    {
        $id = $this->input->get('id');
        $this->db->where('id', $this->input->post('id'));
        
        if ( $this->db->update('ci_pages', $data) )
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    

    }
    // Delete Page Model
    function delete_page()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('ci_pages');
    }
}    
?>
#45

[eluser]danmontgomery[/eluser]
Code:
if($query->num_rows()>0){
    return $query->result_array();
}

This is the problem.

You're returning the result_array() from the method, then trying to get the result_array() again in the controller.

This should be changed to:
Code:
// Get all pages from database
function getAllPages() {

    $qs = "SELECT * FROM ci_pages ORDER BY id";
    return $this->db->query($qs);
}

Or, keep it as it is, and remove the num_rows() and result_array() calls in the controller.
#46

[eluser]the_unforgiven[/eluser]
Right yes that makes sense but still errors coming through:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/admin.php
#47

[eluser]danmontgomery[/eluser]
...What is the line number?

Do some legwork here. What's on that line? The error message tells you that you're trying to get a property of a non object, so what's on that line that you're treating like an object? Where is that variable assigned?

We don't have your code in front of us, we only have what you've posted (without line numbers, mind you), so there's really nothing anybody can do with a vague error message with information stripped out.
#48

[eluser]the_unforgiven[/eluser]
Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/admin.php

Line Number: 117


From line 108
if($query->num_rows()>0)
{
foreach ($query->result_array() as $row)
{
$data['id'] = $this->id;
$data['name'] = $row->name;
$data['description'] = $row->description;
$data['keywords'] = $row->keywords;
$data['content'] = $row->content;
$data['status'] = $row->status;
}
}
#49

[eluser]the_unforgiven[/eluser]
So it looks like its not setting or getting varibles by my thinking, and yes sorry should have posted what I just have along with the error message, apologies for that
#50

[eluser]danmontgomery[/eluser]
Quote:foreach ($query->result_array() as $row)

You're looping through an array, trying to access it as an object.

Code:
foreach($query->result() as $row)

or

Code:
$data['name'] = $row['name'];




Theme © iAndrew 2016 - Forum software by © MyBB