簡體   English   中英

插入特殊字符CodeIgniter的問題

[英]Issues with inserting special characters CodeIgniter

我正在嘗試將mysql查詢本身插入表字段(在滿足某些條件時執行一系列查詢),但是只要查詢中有任何特殊字符,它都會轉換為相應的實體。

例如,如果Iam將此查詢插入到表中,則引號將變為“'”。 ,>到> 和&#的空間。 有什么方法可以按原樣插入查詢並以正確的形式顯示。

    "select
      case item_id
      when 206 then '1 Column'
      when 255 then '2 Columns'
      end as split,
      # case oi.product_id
      #   when 24 then 'XXXX'
      #   when 28 then 'CCCC'
      #   when 30 then 'EEEE'
      #   else 'Something Else'
      # end as product,
     case oi.price_id
       when 72 then 'UYT - Single Pay'
       when 73 then 'UYT - Single Pay'
       when 74 then 'UYT - Single Pay'
     else 'Upsell'
     end as product,
     count(distinct(al.cust_id)) as the_count
     from logtable al
     where item_id in (206,255) and
        activity_dts > '2012-01-31 19:15:00' and
        o.order_is_refunded = 0 and
        t.response_code = 1 and
        t.response_reasontext not like '%testmode%'
        group by   1,2;"

請給我建議,否則我在這里想念任何東西。 我的CI安裝使用的字符集為UTF-8。

PHP添加斜線

此函數將幫助您在數據庫中添加查詢及其引用。

你也可以這樣

如果您的查詢僅具有多個單引號,則將其用雙引號引起來:

$tmp = "See single' quotes";

反之亦然,例如:

$tmp = 'See double " quotes';

嘗試跟隨

 $data_insert = mysql_real_escape_string($values);

您也可以在http://php.net/manual/en/function.mysql-real-escape-string.php中找到詳細信息

插入字符串應該沒有任何問題,因為它字符串,並且數據庫庫本身不會進行任何字符編碼,即afaik。 數據庫表的編碼是什么? (根據默認設置,連接應為UTF-8)

由於使用的是框架,因此可以使用其方法(而不是 mysql_real_escape_string()或上帝,addslashes()!!就像其他答案中建議的那樣),這應該可以工作:

$string_query = "select
      case item_id
      when 206 then '1 Column'
      when 255 then '2 Columns'....";
$sql = "INSERT INTO table(column) VALUES (?)";
$this->db->query($sql, array($string_query));

查詢綁定會自動轉義FOR SQL,因此您很安全(可以將ActiveRecord用於相同的結果)。 我不知道在這種情況下不能工作,肯定沒有功能對html進行編碼。

也許您正在其他地方進行編碼: 確定沒有調用 xss_clean()htmlspecialchars() / htmlentities() ,或者啟用了XSS保護,或者將TRUE作為$this->input->第二個參數傳遞$this->input->

如果以上所有操作由於某種原因(由於您沒有提供足夠的信息)而失敗,則可以對所有內容進行編碼:

$string_query = base64_encode("select.....");
// HERE YOU MAKE THE INSERT QUERY

當您檢索它時,您將base64_decode()字符串。 但是理想的解決方案不是這個,它應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM