0

我正在尝试更新我的数据库。其中一个列值包含撇号等。我$this->db->escape在 CodeIgniter 中使用了可能包含此类字符的字符串,但我仍然收到以下错误:

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 'O\'Keeffe, O\'Keefe'' WHERE `survey_id` = 188' at line 1

UPDATE `survey` SET `firstname_confidence_score` = 100, `firstname_rhymes` = '''', `lastname_confidence_score` = 85, `lastname_rhymes` = ''O\'Keeffe, O\'Keefe'' WHERE `survey_id` = 188;

我该如何解决?

更新:

$sql = "UPDATE `$table_name` SET `firstname_confidence_score` = $firstname_confidence_score, `firstname_rhymes` = '" . $this->db->escape($firstname_rhymes) . "', `lastname_confidence_score` = $lastname_confidence_score, `lastname_rhymes` =  '" . $this->db->escape($lastname_rhymes) . "' WHERE `$primary_id` = $id;";
$result = $this->db->query($sql);
4

2 回答 2

3

由于您使用的是$this->db->escape(),因此您会自动在数据周围添加单引号。

您的查询只需:

 $sql = "UPDATE `$table_name` 
         SET `firstname_confidence_score` = $firstname_confidence_score,  
             `firstname_rhymes` = " . $this->db->escape($firstname_rhymes) . ",  
             `lastname_confidence_score` = $lastname_confidence_score, 
             `lastname_rhymes` =  " . $this->db->escape($lastname_rhymes) . 
        "WHERE `$primary_id` = $id;";

您不需要单引号$this->db->escape($firstname_rhymes)等。

于 2013-03-25T20:21:19.137 回答
1
UPDATE `survey` SET `firstname_confidence_score` = 100, `firstname_rhymes` = '''', `lastname_confidence_score` = 85, `lastname_rhymes` = 'O\'Keeffe, O\'Keefe' WHERE `survey_id` = 188;

您在 lastname_rhymes 值周围有双撇号。

于 2013-03-25T20:06:27.780 回答