【问题标题】:Error when trying to insert data?尝试插入数据时出错?
【发布时间】:2013-08-14 02:17:24
【问题描述】:

尝试将简单查询插入数据库时​​,我不断收到服务器错误。我已经看了一个小时,我找不到问题出在哪里。

    <form action="<?php echo base_url();?>index.php/frontpage/insert_into_db" method="POST">
    <input type = 'text' name='f1' id="searchbar" placeholder="Paste Terminal error here...">
    <input type= 'text' name='f2' id="searchbar" placeholder="Paste Terminal solution here...">
    <input type='submit' id="search" class="btn btn-primary">
    </form>

型号:

   function insert_into_db()
{
    $f1 = $_POST['f1'];
    $f2 = $_POST['f2'];
    $this->db->query("INSERT INTO `Errors` VALUES('$f1' , '$f2')");
    }
}

控制器:

function insert_into_db()
{
    $this->load->model('all');
    $this->all->insert_into_db();
    $this->load->view('success');
}

提前致谢。

【问题讨论】:

  • 你能检查日志并提供具体的消息吗?你还能检查你的表是否有两列以上
  • 如果您能告诉我们什么服务器错误,那将是一个巨大的帮助。另外,f1f2 的内容是什么,因为如果它们包含任何特殊的 SQL 字符,所有的地狱都会崩溃。您真的非常想使用绑定参数(请参阅 ellislab.com/codeigniter/user-guide/database/queries.html 的查询绑定)
  • 它没有说明错误是什么。它只是显示了这个-d.pr/i/Xg1Z
  • 另外,f1 和 f2 的内容不包含任何特殊的 SQL 字符
  • d.pr/i/oQAU 错误信息

标签: php mysql codeigniter


【解决方案1】:

我已将其作为评论发布,因为它似乎不是 OP 的问题,但它恰好是所以我将其作为一个答案。

function insert_into_db()
{
    $data = array('Error' => $this->input->post('f1'),
                  'Solution' => $this->input->post('f2')
                 );
    $this->db->insert('Errors', $data);
}

我主要是直接从 codeigniter 使用 insert 函数和 post 方法,而不是 PHP 的方法。

我使用数组定义 MySQL 表中的列名以及它应该使用的数据,这些数据也将被 codeigniter 正确转义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    相关资源
    最近更新 更多