By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

apostrophe breaking an insert query from import csv file

100+
P: 170
Hi all,

I have the code:

Expand|Select|Wrap|Line Numbers
  1. if(is_uploaded_file($_FILES['file']['tmp_name'])) 
  2.     {
  3.     move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);
  4.     }
  5.  
  6. $fcontents = file ('students.csv'); 
  7. $size=sizeof($fcontents);
  8. for($i=0; $i<sizeof($fcontents); $i++) 
  9.     { 
  10.     $line = trim($fcontents[$i]); 
  11.     $arr = explode(",", $line); 
  12.  
  13.     $sql = "insert into staffcba_students values ('','$user','$pass','$classyear','$classname','$classid','".implode("','",$arr)."')"; 
  14.     mysql_query($sql);
  15.  
  16.     if(mysql_error()) 
  17.         {
  18.         echo mysql_error() ."<br>\n";
  19.               } 
  20.     }
  21.  
The csv file looks like

Peter,Porridge,M
Ethel,Elephant,F

All works perfectly until one of the names has an apostrophe

Eg.
Peter,O'Toole,M

Now I can see why it's breaking it. But I'm not sure how to fix the problem.

I tried addslashes around the trim($fcontents[$i]), but to no avail. Can anyone suggest a simple solution?
Dec 5 '07 #1
Share this Question
Share on Google+
1 Reply


stepterr
100+
P: 157
Take a look at the get_magic_quotes_gpc function. I think that may solve your problem. Link
Dec 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.