插入与帖子ID相关联的评论

时间:2016-04-27 17:19:09

标签: php sql codeigniter model-view-controller

尝试在Report_comment表中插入ReportID和注释到Repot_comments表

但我现在收到错误

 Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0) VALUES ('Report_Comments')' at line 1

INSERT INTO `Report_Comments` (0) VALUES ('Report_Comments')

Filename: models/report/Report_model.php

Line Number: 61

控制器

 function comment_add()
        {
                if ($query = $this->report_model->create_comment()) {

                    $this->session->set_flashdata('messagetwo', 'You added a Comment');
                    redirect('main/comments/' .$_POST['ReportID']);

                } else {
                    $this->session->set_flashdata('messagetwo', 'Sorry not this time');
                    redirect('main/comments/' .$_POST['ReportID']);
                }
        }

型号

function create_comment()
{

    $new_comment = array(
        'Comments', $_POST => $this->input->post('Report_Comments')
    );

    $insert = $this->db->insert('Report_Comments', $new_comment);
    return $insert;
}

查看

    <p><?= anchor('main', 'Back home'); ?></p>

    <?= form_open('main/comment_add'); ?>

    <?= form_hidden('ReportID', $this->uri->segment(3)); ?>

    <p><textarea name="Comments" rows="10"></textarea></p>
    <p><input type="submit" value="add comment"/></p>

    <?php
    if ($this->session->flashdata('messagetwo')) {
        ?>
        <div class="message flash">
            <?php echo $this->session->flashdata('messagetwo'); ?>
        </div>
        <?php
    }
    ?>

    </form>

1 个答案:

答案 0 :(得分:0)

问题是你的:

$new_comment = array(
        'Comments', $_POST => $this->input->post('Report_Comments')
    );

使用var_dump($new_comment)检查,第一个索引应为0;

试试:

$new_comment = array(
        'Comments' => $this->input->post('Report_Comments')
    );

假设你的领域是&#34;评论&#34;并且$this->input->post('Report_Comments')返回一个有效的字符串。

Here is the documentation of $this->db->insert() for codeigniter