douhong1703 2014-07-29 18:02
浏览 53
已采纳

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/');
    }
}
  • 写回答

2条回答 默认 最新

  • dpfad62426 2014-07-29 20:39
    关注

    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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动