CodeIgniter Forums
DB Active Record - addslashes on data? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: DB Active Record - addslashes on data? (/thread-24259.html)



DB Active Record - addslashes on data? - El Forum - 11-04-2009

[eluser]gscharlemann[/eluser]
Hello

Quick question. Is there a simple way to escape the data being inserted via the active record class? Here's the scenario: I have an array of keys and values that is parsed from a CSV file. One of the values is a name field, which inevitably contained a name like: "O'keefe"
Code:
$this->db->insert('mytable', $data);
throws an error on O'keefe, because of the '. How can I run the addslashes() function on all of the values in the $data array? The $data array contains between 300 and 1500 key, value pairs, usually.

Thanks!


DB Active Record - addslashes on data? - El Forum - 11-04-2009

[eluser]gscharlemann[/eluser]
So, I assume the Active Record class doesn't addslashes and remove slashes when querying the DB (although it seems to me like it should, or atleast give you the option of being able to). Perhaps something can be done to all of the values in the array of data. The array is 2D looks something like:
Code:
Array (
    1 => Array (
        "Name" => "Sean O'Keefe"
        "Age" => "13"
    )
    2 => Array (
        "Name" => "Jason Jones"
        ...
    )
)

Can something be done using array_values function? I really don't want to iterate through the entire array and apply the addslashes function to each value


DB Active Record - addslashes on data? - El Forum - 11-04-2009

[eluser]gscharlemann[/eluser]
I posted the above without doing a thorough review of the addslashes documenation for php. Noticed in the comments that someone provided a function to add slashes to the values in an array. I guess that will work, but it doesn't seem to be the most efficient, especially with a large 1500+ item array.

http://php.net/manual/en/function.addslashes.php


DB Active Record - addslashes on data? - El Forum - 11-04-2009

[eluser]├╝berfuzz[/eluser]
Why store a file like that in a sql-row? Store the file on the server. Add slashes if or when you move the rows to a sql-table.


DB Active Record - addslashes on data? - El Forum - 11-04-2009

[eluser]CroNiX[/eluser]
[quote author="gscharlemann" date="1257380606"]Hello

Quick question. Is there a simple way to escape the data being inserted via the active record class? Here's the scenario: I have an array of keys and values that is parsed from a CSV file. One of the values is a name field, which inevitably contained a name like: "O'keefe"
Code:
$this->db->insert('mytable', $data);
throws an error on O'keefe, because of the '. How can I run the addslashes() function on all of the values in the $data array? The $data array contains between 300 and 1500 key, value pairs, usually.

Thanks![/quote]

Ive not had that problem. The manual for the database class says that when using active record with the insert/update methods it automatically escape data, which it does for me.


DB Active Record - addslashes on data? - El Forum - 11-10-2009

[eluser]gscharlemann[/eluser]
Really? it's not working that way for me... maybe I'm doing something wrong. I added the following function to MY_database_helper.php file:

Code:
function _mysql_real_escape_array($t){
    return array_map("mysql_real_escape_string",$t);
}

it seems to work. thanks for the feedback.