PHP issue with uploading a CSV and inserting to database

This was working properly but now I’m having some issues that I’d like to clear up.

[ul][li]I need php to skip header line[/li]
[li]The CSV is only being read for one line, not multiple lines[/li][/ul]

The array seems to have no issues, but If I upload a CSV with a header, it only inserts the header row in the db, and if no header is there it only reads the first line in. I tried a while loop (feof $handle) but it just kept going and froze up the browser on a 6 line CSV. Here is my CSV reading code:

[php]if(isset($_POST[‘submit’]))
{

$file = $_FILES[“file”][“tmp_name”];
$handle = fopen($file, “r”);
$filesop = fgetcsv($handle, 0, “,”);

$coldata = array();

$coldata[ “orderNumber” ] = $filesop[0];
$coldata[ “place” ] = $filesop[1];

//This goes on for 200 array elements, and then I define a table array and my main loop:

$tablenames = array(“staging”);

for($tableno = 0;$tableno < sizeof($tablenames);$tableno++){
$q = “”;
$q2 = “”;
$q3 = “”;
$q4 = “”;
$q5 = “”;
$q6 = “”;
$col_list = ‘'.str_replace(',',',',$table_cols[$tableno]).'’;
$q .= “INSERT INTO “.$tablenames[$tableno].” (”.$col_list.") VALUES (";
$last_id = mysqli_insert_id($connect);
$cols = explode(",",$table_cols[$tableno]);
$data = array();
foreach($cols as $key => $fldname) {
$data[] = “’”.$coldata[$fldname]."’";
}

/*INSERT INTO STAGING TABLE - INITAL CSV UPLOAD*/
$q .= implode(",",$data).");";[/php]

Any help to make sure this avoids the header and reads all lines in is greatly appreciated

Sorry, but cannot help you based on the partial code posted. It doesn’t show how you are executing the query and looping & reading the data from the file. You are also getting the last insert id value before ever executing an insert query, unless that was in some of the code that got cut out.

I tried a while loop (feof $handle) ...

You would need to post that version of the code if you want help with what was wrong with it.

As to avoiding the header line in the file, if it’s always present, you would just read and discard the first line from the file. The easiest way to do this would be to have a loop counter, initialized to one before the start of the loop, then test inside the loop if the counter is a one, post-increment the counter, and skip the execution of the code inside the loop (see the php continue; statement.). If the header is optional, may or may not exist, you would need to devise a test that would distinguish the difference between a header line and a line of data, by identifying a value that would only exist in the header and never in the data, then in the logic testing the first pass through the loop, add a test so that the call to the continue; statement only occurs if the first line is a header.

Also, you should use a prepared query, with the query being prepared once, before the start of any looping, then only supply the data values and execute the query inside of the loop. By using a prepared query, you will save about 5% of the execution time, since the sql query statement will only be sent to the db server once, but more importantly, will prevent any sql special characters in the data from breaking the sql query syntax, which is also how sql injection is accomplished.

Lastly, based on the last insert id statement, you are using the php mysqli extension. If you can, switch to use the php PDO extension. It is much easier to use, especially when using prepared queries and doubly so, when using dynamically produced prepared queries, i.e. exactly what you are doing.

As [member=87768]phdr[/member] stated, there is not enough information for us to help you.

Sponsor our Newsletter | Privacy Policy | Terms of Service