Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中插入值不会显示错误_Php_Mysql_Codeigniter_Post - Fatal编程技术网

Php 在codeigniter中插入值不会显示错误

Php 在codeigniter中插入值不会显示错误,php,mysql,codeigniter,post,Php,Mysql,Codeigniter,Post,我正在尝试使用codeigniter向db插入一行 模型-post.php class Post extends CI_Model{ function get_posts($num=20, $start=0){ $this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($num,$start);

我正在尝试使用codeigniter向db插入一行
模型-post.php

class Post extends CI_Model{
    function get_posts($num=20, $start=0){
        $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->return_id();
    }
控制器-posts.php

class Posts extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('post');
    }
    function index(){
        $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());
        }
        else{
            $this->load->view('new_post');
        }
    }
<form action="<?php base_url(); ?>posts/new_post" method="action">
    <p>Title: <input type="text" name="title"></p>
    <p>Description: <input type="textarea" name="post"></p>
    <input type="submit" value="Add post">
</form>
foreach ($posts as $post) { ?>
<div id-="container">
    <div><h3><a href="<?php base_url(); ?>post/<?php echo $post['postID']; ?>"><?php echo $post['title']; ?> </a></h3>
        <?php echo $post['post']; ?>
    </div>
</div>
<?php
}
查看-new\u post.php

class Posts extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('post');
    }
    function index(){
        $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());
        }
        else{
            $this->load->view('new_post');
        }
    }
<form action="<?php base_url(); ?>posts/new_post" method="action">
    <p>Title: <input type="text" name="title"></p>
    <p>Description: <input type="textarea" name="post"></p>
    <input type="submit" value="Add post">
</form>
foreach ($posts as $post) { ?>
<div id-="container">
    <div><h3><a href="<?php base_url(); ?>post/<?php echo $post['postID']; ?>"><?php echo $post['title']; ?> </a></h3>
        <?php echo $post['post']; ?>
    </div>
</div>
<?php
}

在您的模型中

 function insert_post($newpost){
        $this->db->insert('posts',$newpost);

        // check if the record was added 
        if ( $this->db->affected_rows() == '1' ) {
         // return new id
         return $this->db->insert_id();}

        else {return FALSE;}        
    }
任何用户输入都必须经过验证。如果您使用的是Codeigniter,则使用其表单验证和输入库,如:

$this->input->post('title')
教程中有一个博客帖子示例

否则,在控制器中——检查新的post id是否没有从模型中返回——如果没有返回,则只需转到同一控制器中的错误方法,这样就不会丢失php错误消息

if ( ! $postid = $this->post->insert_post($newpost); ){

   // passing the insert array so it can be examined for errors
   $this->showInsertError($newpost) ;  } 

else { 
      // success now do something else ; 
     } 

整个应用程序中存在许多问题。以下是我发现的:


视图

您的
新帖子中有两个问题

  • 您没有回显您的
    基本url
    。您需要替换窗体的action属性

  • 方法
    属性应具有
    post
    get
    。在这种情况下,它应该是
    post

  • 更改如下:

    function new_post() {
        if ($this->input->post()) {
            $data = array(
                'title' => $this->input->post('title'),
                'post' => $this->input->post('post'),
                'active' => 1
            );
            $id = $this->post->insert_post($data);// this is the id return by your model.. dont know what you wann do with it
            // maybe some conditionals checking if the $id is valid
            redirect(base_url());
        } else {
            $this->load->view('new_post');
        }
    }
    
    由此:

    <form action="<?php base_url(); ?>posts/new_post" method="action">
    

    型号


    函数
    insert_post()
    不应具有
    $this->db->return_id(),它应该是
    $this->db->insert_id()

    我认为您的
    基本url
    设置不正确。如果您转到
    application\config\config.php
    您为
    $config['base\u url']
    设置了什么?
    post\u index.php
    中可能有输入错误,请查看行
    哇哦--return\u id()来自哪里?:-)(更正了我的回答)