2

I can't seem to figure this out but my code is inserting 1 blank row (1st row). The blank row has blank car name, blank car brand, and only has "0.00" in car price. This code is for uploading a csv file and getting the data from that csv file and inserting to database. The first row is the column headers and I was assuming that the first call of $GetHeaders = fgetcsv($file); would have been for the headers.

$file = fopen($_FILES['fileupload']['tmp_name'],"r");

$GetHeaders = fgetcsv($file);
$CarName = array_search('Car Name', $GetHeaders);
$CarBrand = array_search('Car Brand', $GetHeaders);
$CarPrice = array_search('Car Price', $GetHeaders);

$theQue = "";
while(! feof($file))
{
    $GetHeaders = fgetcsv($file);
    $theQue .= "INSERT INTO cardb (CarName, CarBrand, Carprice) VALUES ('$GetHeaders[$CarName]', '$GetHeaders[$CarBrand]', '$GetHeaders[$CarPrice]')";
}

fclose($file);

if (mysqli_multi_query($connection, $theQue))
{
   echo "Success";
}
Jin Pards
  • 189
  • 1
  • 2
  • 13
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Sep 23 '15 at 18:49
  • show us some sample data – Funk Forty Niner Sep 23 '15 at 18:56
  • *By the time you see this Sam, this tab would've already been closed.* - @JayBlanchard – Funk Forty Niner Sep 23 '15 at 19:06
  • Hello, this is a sample script but similar to the actual, I had to remove the real data there for security reasons. I understand it is at risk of SQL injections but the plan is to construct this first, get the basics to work then do the security measures.:) – Jin Pards Sep 23 '15 at 23:17

2 Answers2

0

A couple things could be causing this.. First of all try to delete your header row in your csv file. Next put in a check that the data row is not equal to blank or null, before writing it to the database.

<?php 
//open the csv file for reading  
$jhandle = fopen($file_path, 'r');  
$row_limit=1000;                
while (($jdata = fgetcsv($jhandle, $row_limit, ",")) !== FALSE) {
$car_name = $jdata[0];  
$car_brand = $jdata[1]; 
$car_price = $jdata[2]; 
If(($car_name != '')||($car_brand != '')||($car_price > 0)){
//write to your database here.
}
//close your while statement
}
  • 1
    Why should the OP try this? A good answer will always have an explanation of what was done (with an example) and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Sep 23 '15 at 19:00
  • I can't remove the headers or first row, I need it to identify each columns. My idea is the problem is in fgetcsv. Can we only call fgetcsv() inside a loop or something? In my case I first called fgetcsv() once to get the headers then in a loop to get the rows. – Jin Pards Sep 23 '15 at 23:20
  • Jay.. you are not helping either.. sitting there calling people out is ZERO help so unless you want to help.. just keep your comments to your self. We ALL appreciate it. – Ryan Churchill Sep 24 '15 at 15:41
  • @RyanChurchill Wrong. He is helping. An answer without a good explanation helps no one. You obviously did agree with Jay in the long run, since you did edit in more info. – patricksweeney Sep 24 '15 at 15:59
  • I thought this was a place to get help to solve problems.. not a place to just have people code for you. My original response gave direction on to how to accomplish this. I later edited it to better illustrate the guidance I had given, and to show the direction I gave would work. – Ryan Churchill Sep 25 '15 at 13:32
0

Posting and ending this with this findings so others who encounter the issue with fgetcsv will get this hint.

This is weird, but after a couple more testing, I found that fgetcsv is reading each rows from the CSV file but there's an additional row being read by fgetcsv which is a NULL.

It's as if there's a invisible row and it's the last row. It is only showing in the first row when in database probably because of auto sort or something but it again the last row fgetcsv got is a NULL. I thought it should have detected NULL as EOF?

What I did to detect the bug is something I should have done in the first place which is to use echo vardump which displayed all the car names and the last car was named "NULL"

Thank you for the help guys, each of you gave me ideas which led me to finding this prick lol

Jin Pards
  • 189
  • 1
  • 2
  • 13