Jump to content

[SOLVED] upload a csv file insert into db stops when it encounters an apostrophe


tryingtolearn

Recommended Posts

Hi again all,

I am trying to upload a csv file and insert the data into a DB

 

But when I encounter an apostrophe in the data it stops

Is there a way around this?

 

Code Im using

      require_once ('../mysql_connect.php'); // Connect to the database.
  $filename = $path.$date.$HTTP_POST_FILES['userfile']['name'];
  
  $handle = fopen("$filename", "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {    
  
       $import="INSERT into leads (prfname2,prlname2,prcaddr12,prcaddr22,prccity2,prcstate2,prcpostco2,prevntdate,prcreated,prhomepho2,premail2,add1,add2,add3,add4) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','Y','2','3','4')";
       mysql_query($import) or die(mysql_error());
   
      }
      fclose($handle);

 

For example - everything goes good and then say an address field in the csv file is

555 ALEXANDER'S OAK

 

It stops right there w/ an error.

 

Any help is appreciated.

Link to comment
Share on other sites

Thanks for the fast reply phpzone

Unfortunetly that didnt work

 

I changed the code to look like this

require_once ('../mysql_connect.php'); // Connect to the database.
  $filename = $path.$date.$HTTP_POST_FILES['userfile']['name'];
  
  $handle = fopen("$filename", "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {    
       $import="INSERT into leads (prfname2,prlname2,prcaddr12,prcaddr22,prccity2,prcstate2,prcpostco2,prevntdate,prcreated,prhomepho2,premail2,add1,add2,add3,add4) values('mysql_real_escape_string($data[0])','mysql_real_escape_string($data[1])','mysql_real_escape_string($data[2])','mysql_real_escape_string($data[3])','mysql_real_escape_string($data[4])','mysql_real_escape_string($data[5])','mysql_real_escape_string($data[6])','mysql_real_escape_string($data[7])','mysql_real_escape_string($data[8])','mysql_real_escape_string($data[9])','mysql_real_escape_string($data[10])','Y','2','3','4')";
       mysql_query($import) or die(mysql_error());
   //print_r($data);
      }
      fclose($handle);

 

But I still get this error

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 'S OAK)','mysql_real_escape_string()','mysql_real_escape_string(CINCINNATI)','mys' at line 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.