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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧