views:

24

answers:

3

Hello,

I am uploading email addresses from a CSV file, via fgetcsv(), into a MySQL database. The problem that I am running into is that I cannot escape email addresses containing an apostrophe (or single quote).

So, O'[email protected] will not become O\'[email protected]

I know that this is supposed to happen, however I have been unable to find a working solution to escape the apostrophe. Can someone offer me any help? Here is my code:

while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
            {

                $num = count($data);
                $row++;
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    $clean_value = cleanse($data[$c]);

                    if(validEmail($clean_value)){
                        $query = mysql_query("INSERT INTO addresses (lid, email_addresses) VALUES ('$lid', '$clean_value')");
                    }else{
                        if($clean_value != ""){
                            $invalid_addresses .= $clean_value.', ';
                        }
                    }

                    /*if(mysql_error()){
                        $failed_addresses .= $data[$c].', '.mysql_error() . "<br />\n";
                    }*/
                }
            }

Thanks!


$query = mysql_query("INSERT INTO addresses (lid, email_addresses) VALUES ('$lid', \"". mysql_real_escape_string($clean_value) ."\")");

...This doesn't work for me

+1  A: 

I don't know what your cleanse function does exactly, but why not use mysql_real_escape_string? That's what it's there for.

Peter Kruithof
My cleanse function removes any NON-ASCII characters from an email address
behrk2
If your function removed non-ascii characters, than the remaining single quote should be escaped this way. Are you sure this is not a special character (maybe pasted from Word)? I've tested this and it should work:`echo mysql_real_escape_string("O'[email protected]"); // O\'[email protected]`
Peter Kruithof
A: 

Use mysql_real_escape_string.

Sjoerd
Hi Sjoerd - I tried using mysql_real_escape_string, and still no backslash appears on the single quote
behrk2
A: 

i don't know if singlequotes in E-Mail addresses are valid, but look at this(your) line:

$query = mysql_query("INSERT INTO addresses (lid, email_addresses) VALUES ('$lid', '$clean_value')");

try to use it this way (is lid a string?):

$query = mysql_query("INSERT INTO addresses (lid, email_addresses) VALUES (".$lid.",\"". mysql_real_escape_string($data[$c])."\")");
helle
Hi helle, single quotes are indeed allowed in email addresses. I tried your suggestion, and I am still not getting an escaped single quote...
behrk2
* changed singlequotes to escaped double quotes. now i tryed it out on my mysql db and it worked fine... sorry, i can't do more for you. You might have some encoding problem.
helle
p.s. this String is the exact result of mysql_real... of your posted string: O\'[email protected] mb_detect_encoding() to find out the encoding.
helle
Hm, I tried that and still no-go. Used mb_detect_encoding() and every email address is ASCII.
behrk2
Here is what my cleanse() is doing: preg_replace('/[^(\x20-\x7F)]*/','', $str);
behrk2