Jump to content

[SOLVED] Need Help With fgetcsv


dlebowski

Recommended Posts

Below is my code for fgetcsv.  If a column in my import contains an apostrophe ' , then it errors out.  It breaks my column separation.  Does anyone know how I could remove apostrophes prior to being imported or can I some how write the query to ignore the apostrophe.  Thanks in advance!

 

if(isset($_POST['submit3']))
   {
     $fileupload=$_POST['fileupload'];
     $filename="$fileupload";
     $auctiondate=$_POST['auctiondate'];
     $handle = fopen("$filename", "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
       $column1=$_POST['column1'];
       $column2=$_POST['column2'];
       $column3=$_POST['column3'];
       $column4=$_POST['column4'];
       $column5=$_POST['column5'];
       $column6="$auctiondate";
       $onlineonsite=ONSITE;
       $lotpaymentexempt=NO;
       
       $import="INSERT into test(LotID,LotNumber,LotTitle,SellingPrice,Buyer,LotPaymentExempt,OnlineOnsite,AbsenteeBid,SellerNumber,LotAuctionDate) values('', '$data[$column1]','$data[$column2]', '$data[$column3]','', '$lotpaymentexempt', '$onlineonsite', '$data[$column4]', '$data[$column5]', '$column6')";
       mysql_query($import) or die(mysql_error());
     }
     fclose($handle);

Link to comment
Share on other sites

Not sure what to say... if you call addslashes() on all the input you're goin to put into the query, it shouldn't complain... can you make the script echo the INSERT SQL statement before trying to do it(e.g., echo $import;), the post what it echoed?

Link to comment
Share on other sites

<?php
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
       $column1=$_POST['column1'];
       $column2=$_POST['column2'];
       $column3=$_POST['column3'];
       $column4=$_POST['column4'];
       $column5=$_POST['column5'];
       $column6="$auctiondate";
       $onlineonsite=ONSITE;
       $lotpaymentexempt=NO;
       
       $import="INSERT into test(LotID,LotNumber,LotTitle,SellingPrice,Buyer,LotPaymentExempt,OnlineOnsite,AbsenteeBid,SellerNumber,LotAuctionDate)";
       $import .=" values('', '" . cleancvs($data[$column1]) . "','" .  cleancvs($data[$column2]) . "',"; 
       $import .= " '" . cleancvs($data[$column3]) . "','', '$lotpaymentexempt', '$onlineonsite', '" . cleancvs($data[$column4]) . "', '" . cleancvs($data[$column5]) . "', '$column6')";
       mysql_query($import) or die(mysql_error());
}
fclose($handle);

function cleancvs($input) {
return(str_replace("'", '', $input));
//OR
//	return(mysql_real_escape_string($input));
}

 

Replace str_replace with mysql_real_escape_string if you want to keep the single quotes.

 

The issue here is not the CVS. It's the SQL. And the slashes added from addslashes or mysql_real_escape_string will not be inputted. That's just a way of telling MySQL that they are apart of the input (and you aren't trying to quote a string). You do NOT need to use stripslashes on output.

Link to comment
Share on other sites

I went ahead and implemented the changes.  It doesn't error out now, but nothing imports.  I will keep playing with the code.  Thanks for helping me out.

You may want to try echoing $import before it hits mysql_query. It's probably another problem with the SQL statement.
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.