I've had no issue with the first line of a CSV being read upon upload and then inserted into the database, but the multi line task is having issues. I can now get it to print an array dump on the web page that shows all lines of the CSV but it's still only inserting the first line into my database. I've worked my code around and came up with this:
if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
while(!feof($handle)){
$filesop = print_r(fgetcsv($handle, 0, ","));
}
$coldata = array();
I have this code for calling the queries (there are other queries after this, but this is the main one that puts into that staging table):
$tablenames = array("staging"/*,"clients","meters","tests","costs","workorders"*/);
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).");";
Am I doing something wrong in the queries that's causing it to not insert all lines of the CSV into the db?