【问题标题】:Error no 1064 in Codeigniter while entering the data with special character " or '输入带有特殊字符“或”的数据时,Codeigniter 中出现错误 1064
【发布时间】:2014-02-19 07:19:15
【问题描述】:

当我在 Artist_as 字段中输入任何特殊字符时出现错误。 CodeIgniter 在输入带有特殊字符 "' 的数据时出现错误 no 1064

这是我的型号代码:

$this->db->query("Delete from tv_cast where tv_id = '{$tvid}'");
if(array_key_exists('tv_cast_as_artist_id', $post)){
    $count = count($post['tv_cast_as_artist_id']);
    $i = 0;
    $this->db->query("Delete from tv_cast where tv_id = '{$tvid}'");
    while($i < $count){
        $this->db->query("Insert into tv_cast (tv_id, artist_id,artist_as)
                Values ('{$tvid}','{$post['tv_cast_as_artist_id'][$i]}','{$post['tv_cast_as_artist_as'][$i]}'
        )");
        $i++;
    }
}

我得到的错误:

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的“s Ex-Boyfriend' )”附近使用正确的语法

Insert into tv_cast (tv_id, artist_id,artist_as) Values ('29','7174','Rashmi's Ex-Boyfriend' )

文件名:/home/divazmed/public_html/design/models/admin_model.php

行号:612

【问题讨论】:

  • 错误描述非常不言自明。使用variablebinding 来避免这样的陷阱。

标签: php sql


【解决方案1】:

您的 artist_as 字段似乎没有正确转义。

使用类似的东西:

$data = array(
   'tv_id' => $tvid ,
   'artist_id' => $post['tv_cast_as_artist_id'][$i] ,
   'artist_as' => $post['tv_cast_as_artist_as'][$i]
);

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

您的所有值都将自动转义,从而产生更安全的查询等

您可以在此处阅读有关 Codeigniter Active Record 类的更多信息:

http://ellislab.com/codeigniter/user-guide/database/active_record.html#insert

【讨论】:

    【解决方案2】:

    在mysql中添加数据: html_entities(mysql_real_escape_string($variable));

    对于页面上的打印,您可以使用: html_entity_decode(stripslashes($variable));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2015-07-15
      • 1970-01-01
      • 2017-10-06
      相关资源
      最近更新 更多