【问题标题】:inserting a value in codeigniter shows no error在 codeigniter 中插入一个值没有显示错误
【发布时间】:2015-04-20 03:36:49
【问题描述】:

我正在尝试使用 codeigniter 向数据库中插入一行。
模型-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');
        }
    }

查看-new_post.php

<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>

索引视图-post_index.php

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
}

索引页面显示来自 db 的所有帖子。单击标题后,post.php 视图将显示相应的数据。这部分很好。
尝试在 new_post.php 中添加新帖子时,它没有反映在数据库中,也没有显示任何错误。
我还使用redirect_url 在插入后重定向到索引页面。所以它显示了相同的可用帖子。单击标题时,它会不断将 posts/post 重复添加到 url。
重定向网址后点击标题一次显示

http://localhost/Codeigniter/posts/posts/post/1

再次点击它添加的标题

http://localhost/Codeigniter/posts/posts/post/post/1

谁能帮助我?谢谢!

【问题讨论】:

  • 我认为您的base_url 设置不正确。如果你去application\config\config.php你为$config['base_url']设置了什么??
  • post_index.php可能有错别字,请看&lt;div id-="container"&gt;这一行

标签: php mysql codeigniter post


【解决方案1】:

在你的模型中

 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')

教程https://ellislab.com/codeIgniter/user-guide/tutorial/create_news_items.html中有一个博客文章示例

否则在您的控制器中-检查新的帖子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 ; 
     } 

【讨论】:

    【解决方案2】:

    整个应用程序存在许多问题。这些是我发现的:


    观看次数

    new_post 视图中有两个问题。

    1. 您没有回显您的 base_url 。您需要替换表单的 action 属性。

    2. method 属性应具有post 或get。在这种情况下,它应该是post

    改成这样:

    从这里:

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

    到这里:

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

    您也可以这样做:

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

    控制器

    在您的posts 控制器中,您的new_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');
        }
    }
    

    型号

    函数insert_post()不应该有$this-&gt;db-&gt;return_id();,而应该是$this-&gt;db-&gt;insert_id();

    【讨论】:

    • 哎呀——return_id() 是从哪里来的? :-)(更正了我的答案)
    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 2016-12-13
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多