CodeIgniter:在更新表单上提交空白屏幕。 没有错误

问题描述:

I'm new to CI and just trying to build a basic blog for practice. Everything works fine (pull all posts, individual posts, add new post, delete post) except for the update posts. The update form loads fin, but When I submit the form I get a blank page with no errors and nothing gets updated on the db. I've searched stack for solutions, but none of these seem to make a difference. Any help would be greatly appreciated.

EDIT: Now spits the following error:

Fatal error: Call to undefined method Post::where() in /Applications/MAMP/htdocs/CodeIgniter_2.2.0/application/models/post.php on line 26

Which is the first line inside the update_post model function:

function update_post($postID, $data){
        $this->where('postID', $postID);
        $this->db->update('posts', $data);
    }

HTML form markup from the view edit_post.php:

Add new post

    <?php if($success==1){ ?>
        <p>Post successfully updated</p>
    <? } ?>

    <form action="<?=base_url()?>posts/editpost/<? echo $post['postID']?>" method="post">
        <p>Title
        <input name="title" type="text" value="<?=$post['title']?>" />
        </p>
        <p>Body
        <textarea name="post"><?=$post['post']?></textarea>
        </p>
        <p><input type="submit" value="Edit Post" /></p>
    </form>

Models:

class Post extends CI_Model{

    function get_posts($num=20, $start=0){
        //$sql="SELECT * FROM users WHERE active=1 ORDER BY date_added DESC LIMIT 0,20;";
        $this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_post($postID){
        $this->db->select()->from('posts')->where(array('active'=>1,'postID'=>$postID))->order_by('date_added','desc');
        $query=$this->db->get();
        return $query->first_row('array');
    }

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

    function update_post($postID, $data){
        $this->where('postID', $postID);
        $this->db->update('posts', $data);
    }

    function delete_post($postID){
        $this->db->where('postID', $postID);
        $this->db->delete('posts');
    }
}

Controller:

class Posts extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->model('post');
    }

    function index(){
        $this->load->model('post');
        $data ['posts']=$this->post->get_posts();
        $this->load->view('post_index',$data);
    }

    function post($postID){
        $data['post'] = $this->post->get_post($postID);
        $this->load->view('post',$data);
    }

    function new_post(){
        if($_POST){
            $data = array(
                'title'=>$_POST['title'],
                'post'=>$_POST['post'],
                'active'=>1

            );
            $this->post->insert_post($data);
            redirect(base_url().'posts/');
        }else{
            $this->load->view('new_post');
        }
    }

    function editpost($postID){
        $data['success']=0;
        if($_POST){
            $data_post=array(
                'title'=>$_POST['title'],
                'post'=>$_POST['post'],
                'active'=>1
            );
            $this->post->update_post($postID, $data);
            $data['success']=1;
        }
        $data['post']=$this->post->get_post($postID);
        $this->load->view('edit_post', $data);
    }

    function deletepost($postID){
        $this->post->delete_post($postID);
        redirect(base_url().'posts/');
    }
}

You missed the db call before where. Here:

function update_post($postID, $data){
        $this->where('postID', $postID);
        $this->db->update('posts', $data);
    }

Replace:

function update_post($postID, $data){
        $this->db->where('postID', $postID);
        $this->db->update('posts', $data);
    }

you can try to debug it by using the below line in the model function

echo $this->db->last_query();

It will print the last query executed, and you can try to run it manually to check whether the query is fine or not, or try to debug it by putting logs. OR add

error_reporting(E_ALL); 

In the controller to display all the errors.