【问题标题】:Insert comment linked to post ID [duplicate]插入链接到帖子 ID 的评论 [重复]
【发布时间】:2016-04-27 17:19:09
【问题描述】:

尝试将 ReportID 和评论插入到repot_cmets 表中

但是我现在遇到错误

 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>

【问题讨论】:

  • 重复的也适用于列名。
  • 它不是重复的,我删除了旧问题,因为我觉得它没有意义而且一团糟。我已经重写了问题并更改了代码。
  • right syntax to use near '0) 并且您的查询是这个(0) VALUES ('Report_Comments'),因此它是重复的。
  • 能分享一下你的表结构吗
  • 在列名周围使用标记`,它会解决您的问题。

标签: php sql codeigniter model-view-controller


【解决方案1】:

问题是你的:

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

检查var_dump($new_comment),第一个索引应该是0;

试试看:

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

假设您的字段是“评论”并且$this-&gt;input-&gt;post('Report_Comments') 返回一个有效字符串。

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

【讨论】:

  • 这是错误,我将如何在代码中执行此操作?
  • 我编辑了我的答案,希望对您有所帮助。
猜你喜欢
  • 2022-01-10
  • 2017-12-19
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多