无法从发布的数据中检索和识别

时间:2014-10-30 19:32:11

标签: php mysql sql codeigniter

我正在尝试在一个笑话线程上添加评论,但是我在抓取应该用来修改帖子评论的joke_id时遇到了麻烦。

我遇到的问题是,没有注释的线程不会将joke_id添加到数据库,并且带有注释的线程工作正常。我不明白为什么。

这是我所说的一个有效例子:

http://ehustudent.co.uk/cis21732825/cis3122/jokehut_4/read/joke/14

这个线程完全正常工作(我只是为了测试目的强调了joke_id)

并且此线程不起作用:

http://ehustudent.co.uk/cis21732825/cis3122/jokehut_4/read/joke/4

评论和名称被添加到数据库中,但是joke_id下降为'0',我不明白为什么。

以下是我在视图中的代码:

    <?php

    //assigning values
    foreach($results as $row){
        $name = $row->name;
        $joke = $row->joke;
        $joke_id = $row->joke_id;
        $date_added = $row->date_added;
        $vote = $row->vote;

    ?>


    }

        echo form_open('comments/insertComment');

        ?>

        <div class="new-com-bt">
            <span>Write a comment ...</span>
        </div>

        <div class="new-com-cnt">
            <input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
            <textarea  required="yes" class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
            <input type="hidden" name="joke_id" value="<?= $joke_id; ?>">

            <input class="bt-add-com" type="submit" value="Post comment">
            <div class="bt-cancel-com">Cancel</div>
        </div>

        <div class="clear"></div>


    <?php

    echo form_close(); ?>

这是我的控制器:

public function index(){

    $this->getComment();

}

public function getComment(){

    $data['results'] = $this->comments_m->getComments();

    $this->load->view('template/header');
    $this->load->view('template/sidebar');
    $this->load->view('content/comment_box', $data);
    $this->load->view('template/footer');

}

public function insertComment(){

    $data = array (
        'user' => 'user',
        'comment' => 'comment',
        'joke_id' => 'joke_id',
        'id_post' => 'joke_id'
    );

    $joke_id = $this->input->post('joke_id');

    $this->comments_m->insertComment($data);
    redirect(base_url('read/joke/'.$joke_id));

}

}

模特:

 //gets the comments
    function getComments ($joke_id){
        $query = $this->db->query("SELECT c.name, j.*, co.* FROM jokes j LEFT JOIN category c ON c.category_id = j.category_id LEFT JOIN comments co ON co.joke_id = j.joke_id WHERE j.joke_id =  '$joke_id' ") or die("No results found" );

        if ($query->num_rows > 0) {
            return $query->result();
        }
    }

    //inserts the comments
    function insertComment (){

        $data = array (
            'user' => $this->input->post('name-com'),
            'comment' => $this->input->post('the-new-com'),
            'joke_id' => $this->input->post('joke_id'),
            'id_post' => $this->input->post('joke_id')
        );

        if(strlen($data['user']) < 1){
            $data['user'] = "Guest";
        }

        $this->db->insert('comments', $data);


    }

评论表:

CREATE TABLE IF NOT EXISTS `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` varchar(40) NOT NULL,
  `comment` text NOT NULL,
  `joke_id` int(11) NOT NULL,
  `id_post` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

笑话表:

CREATE TABLE IF NOT EXISTS `jokes` (
  `joke_id` int(11) NOT NULL AUTO_INCREMENT,
  `joke` varchar(1024) NOT NULL,
  `category_id` int(11) NOT NULL,
  `vote` int(255) NOT NULL,
  `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

为什么joke_id没有在没有笑话的页面上插入/显示的任何帮助都会很棒。

谢谢

0 个答案:

没有答案