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
https://forums.phpfreaks.com/topic/65585-solved-need-help-with-fgetcsv/
Share on other sites

Thanks for the quick reply.  It still isn't getting past that.  It is breaking on this line if I manually remove the apostrophe in "Torch's" prior to the import, it works:

 

1, "Snap-On Torch's Bit Sockets", 8 Pieces, 2, "0000-00-00"

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?

<?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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.