0

I have an array in my PHP code that has about 200 column elements. It looks like this:

$coldata = array();

$coldata[ "orderNumber" ] = $filesop[0];
$coldata[ "place" ] = $filesop[1];
$coldata[ "workOrderNum" ] = $filesop[2];
$coldata["lowSideMIUNum"] = $filesop[3];
$coldata["highSideMIUNum"] = $filesop[4];
$coldata["accountNum"] = $filesop[5];
$coldata["custName"] = $filesop[6];
$coldata["address"] = $filesop[7];
$coldata["locID"] = $filesop[8];

Like I said, this continues on to about 199/200. I found out today that the CSV will maintain this order but they've added about 30 fields throughout in random orders. Some will be in the first 20 and some between 110 and 120. Is there a better practice to add new elements here? I've already added them in the database where they need to be but now I need to put them in the correct place in the array and I don't know if there's a way without re-numbering the whole array.

UPDATE Full code, except where long or repetitive elements are coded

<?php

$server = "localhost";
$user = "root";
$pw = "root";
$db = "uwsTest";

$connect = mysqli_connect($server, $user, $pw, $db);

if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
    echo'success!';
}


if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);

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


$filesop = fgetcsv($handle, 0, ",");

$coldata = array();

$coldata[ "orderNumber" ] = $filesop[0];
$coldata[ "place" ] = $filesop[1];
$coldata[ "workOrderNum" ] = $filesop[2];   
$table_cols = array();

/*staging*/
$table_cols[0] = //200 element array code omitted

$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).");";

/*INSERT INTO CLIENTS TABLE FROM STAGING TABLE*/
$q2 .= "INSERT INTO clients (orderNumber,place,workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,address2,servicePreformed)
 SELECT orderNumber,place,workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,address2,servicePreformed
 FROM staging
 WHERE StageID = (SELECT MAX(StageID)FROM staging);";

 /*INSERT INTO METERS TABLE FROM STAGING TABLE*/
 $q3 .= "INSERT INTO meters (workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName,bypassSize,meterSize,meterType,manufacturer,registration,technician,linePressurePSI,lat,lon,lowSideRrBefore,highSideRrBefore,firesideRrBefore,lowSideRrAfter,highSideRrAfter,firesideRrAfter,vgOxygen,vgCombustGas,vgCarbonMon,vgHydroSulf)
 SELECT workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName,bypassSize,meterSize,meterType,manufacturer,registration,technician,linePressurePSI,lat,lon,lowSideRrBefore,highSideRrBefore,firesideRrBefore,lowSideRrAfter,highSideRrAfter,firesideRrAfter,vgOxygen,vgCombustGas,vgCarbonMon,vgHydroSulf 
 FROM staging
 WHERE StageID = (SELECT MAX(StageID)FROM staging);";

 /*INSERT INTO TESTS TABLE FROM STAGING TABLE*/
 $q4 .= "INSERT INTO tests (workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,test1TestRateGPM,test1MeterVol,test1TesterVol,test1Accuracy,test1CorrectAcc,test2TestRateGPM,test2MeterVol,test2TesterVol,test2Accuracy,test2CorrectAcc,test3TestRateGPM,test3MeterVol,test3TesterVol,test3Accuracy,test3CorrectAcc,test4TestRateGPM,test4MeterVol,test4TesterVol,test4Accuracy,test4CorrectAcc,test5TestRateGPM,test5MeterVol,test5TesterVol,test5Accuracy,test5CorrectAcc,test6TestRateGPM,test6MeterVol,test6TesterVol,test6Accuracy,test6CorrectAcc,test7TestRateGPM,test7MeterVol,test7TesterVol,test7Accuracy,test7CorrectAcc,test8TestRateGPM,test8MeterVol,test8TesterVol,test8Accuracy,test8CorrectAcc,inletValveSize,InletValveType,inletValveCond,outletValveSize,outletValveType,outletValveCond,bypassValveSize,bypassValveType,bypassValveCond,vaultLength,vaultWidth,vaultHeight,meterLocation,testPort,testPortInstalled,testPortSize,picture,comments,testResults,retest,test1TestRateGPM2,test1MeterVol2,test1TesterVol2,test1Accuracy2,test1CorrectAcc2,test2TestRateGPM2,test2MeterVol2,test2TesterVol2,test2Accuracy2,test2CorrectAcc2,test3TestRateGPM2,test3MeterVol2,test3TesterVol2,test3Accuracy2,test3CorrectAcc2,test4TestRateGPM2,test4MeterVol2,test4TesterVol2,test4Accuracy2,test4CorrectAcc2,test5TestRateGPM2,test5MeterVol2,test5TesterVol2,test5Accuracy2,test5CorrectAcc2,test6TestRateGPM2,test6MeterVol2,test6TesterVol2,test6Accuracy2,test6CorrectAcc2,test7TestRateGPM2,test7MeterVol2,test7TesterVol2,test7Accuracy2,test7CorrectAcc2,test8TestRateGPM2,test8MeterVol2,test8TesterVol2,test8Accuracy2,test8CorrectAcc2)
 SELECT workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,date,utility,serialNumber,serviceName,test1TestRateGPM,test1MeterVol,test1TesterVol,test1Accuracy,test1CorrectAcc,test2TestRateGPM,test2MeterVol,test2TesterVol,test2Accuracy,test2CorrectAcc,test3TestRateGPM,test3MeterVol,test3TesterVol,test3Accuracy,test3CorrectAcc,test4TestRateGPM,test4MeterVol,test4TesterVol,test4Accuracy,test4CorrectAcc,test5TestRateGPM,test5MeterVol,test5TesterVol,test5Accuracy,test5CorrectAcc,test6TestRateGPM,test6MeterVol,test6TesterVol,test6Accuracy,test6CorrectAcc,test7TestRateGPM,test7MeterVol,test7TesterVol,test7Accuracy,test7CorrectAcc,test8TestRateGPM,test8MeterVol,test8TesterVol,test8Accuracy,test8CorrectAcc,inletValveSize,InletValveType,inletValveCond,outletValveSize,outletValveType,outletValveCond,bypassValveSize,bypassValveType,bypassValveCond,vaultLength,vaultWidth,vaultHeight,meterLocation,testPort,testPortInstalled,testPortSize,picture,comments,testResults,retest,test1TestRateGPM2,test1MeterVol2,test1TesterVol2,test1Accuracy2,test1CorrectAcc2,test2TestRateGPM2,test2MeterVol2,test2TesterVol2,test2Accuracy2,test2CorrectAcc2,test3TestRateGPM2,test3MeterVol2,test3TesterVol2,test3Accuracy2,test3CorrectAcc2,test4TestRateGPM2,test4MeterVol2,test4TesterVol2,test4Accuracy2,test4CorrectAcc2,test5TestRateGPM2,test5MeterVol2,test5TesterVol2,test5Accuracy2,test5CorrectAcc2,test6TestRateGPM2,test6MeterVol2,test6TesterVol2,test6Accuracy2,test6CorrectAcc2,test7TestRateGPM2,test7MeterVol2,test7TesterVol2,test7Accuracy2,test7CorrectAcc2,test8TestRateGPM2,test8MeterVol2,test8TesterVol2,test8Accuracy2,test8CorrectAcc2
 FROM staging
 WHERE StageID = (SELECT MAX(StageID)FROM staging);";

 /*INSERT INTO COSTS TABLE FROM STAGING TABLE*/
 $q5 .= "INSERT INTO costs (workOrderNum,onsiteSurveyTestCost,onsiteSurveyTestRepairCost,offsiteSurveyTestCost,offsiteSurveyTestRepairCost,onsiteTestOnlyCost,onsiteTestRepairOnlyCost,onsiteRepairOnly,testPort2,repairCompleteMeterReplacement,repairCompleteMeterReplacementLaborCost,umeCost,umeLaborCost,rotatingLowSideDiskChamber,rotatingLowSideDiskChamberLaborCost,turbineChamberCost,turbineChamberLaborCost,automaticValveCost,automaticValveLaborCost,strainerCost,strainerLaborCost,lowRegisterCost,lowRegisterLaborCost,highRegisterCost,highRegisterLaborCost,miuCost,miuLaborCost,totalCost)
 SELECT workOrderNum,onsiteSurveyTestCost,onsiteSurveyTestRepairCost,offsiteSurveyTestCost,offsiteSurveyTestRepairCost,onsiteTestOnlyCost,onsiteTestRepairOnlyCost,onsiteRepairOnly,testPort2,repairCompleteMeterReplacement,repairCompleteMeterReplacementLaborCost,umeCost,umeLaborCost,rotatingLowSideDiskChamber,rotatingLowSideDiskChamberLaborCost,turbineChamberCost,turbineChamberLaborCost,automaticValveCost,automaticValveLaborCost,strainerCost,strainerLaborCost,lowRegisterCost,lowRegisterLaborCost,highRegisterCost,highRegisterLaborCost,miuCost,miuLaborCost,totalCost 
 FROM staging
 WHERE StageID = (SELECT MAX(StageID)FROM staging);";

 /*INSERT INTO WORKORDERS TABLE FROM STAGING TABLE*/
 $q6 .= "INSERT INTO workorders (workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName)
 SELECT workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address,locID,utility,serialNumber,serviceName 
 FROM staging
 WHERE StageID = (SELECT MAX(StageID)FROM staging);";

 /*DEBUG ALL SQL QUERIES*/
echo "<p>\$q:<pre>".print_r($q,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q2,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q3,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q4,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q5,true)."</pre></p>\n";
echo "<p>\$q:<pre>".print_r($q6,true)."</pre></p>\n";
}

/*CONNECT INITAL QUERY AND GET ERROR*/
if(mysqli_multi_query($connect, $q)) {echo'File submitted'; } else {     /*var_dump($q)*/echo "Error: " . mysqli_error($connect); }

/*CONNECT OTHER QUERIES - NO ERROR PRINTING*/
mysqli_multi_query($connect,$q2);
mysqli_multi_query($connect,$q3);
mysqli_multi_query($connect,$q4);
mysqli_multi_query($connect,$q5);
mysqli_multi_query($connect,$q6);
}

?>
20
  • 1
    Do they send you the column names in the first row? If so, key off that and abandon the numeric index. If they don't, you'll either have to manually sync them. Commented Apr 5, 2017 at 21:25
  • 1
    As @mkaatman asked, does the CSV have the column names in the first row? If so, that would allow you to greatly automate the process and help detect future changes. Commented Apr 5, 2017 at 21:33
  • 1
    Show the code! You don't have to show all 200 lines of the array, but show all of the code you are using to import the data. Commented Apr 6, 2017 at 14:28
  • 1
    When you read the first line containing the column names, if they don't match the names of the table columns, you could have an array with the keys matching the column names in the CSV, and the values matching the names of the columns. No need after that to maintain the array like in your OP. Also, since you would create the SQL based on the array, the order of the fields doesn't matter when they make changes to the CSV Commented Apr 6, 2017 at 14:30
  • 1
    I'm not sure why you use the line $filesop = print_r(fgetcsv($handle, 0, ",")); What's the purpose of the print_r? Having the fields in an array would sure make the import easier. Also, you define $q2 - $q6, but never populate them. Although I don't see anything apparent in the insert statement that would be a problem, it's difficult to know considering how it's built. Echo the statement, and try it in phpAdmin to see what errors you might get. Also paste it here so I can take a look at it. Commented Apr 6, 2017 at 18:55

1 Answer 1

1

Import a CSV file with the first line containing column names.

Allows you to define a translation between the column names in the CSV and the column names in the table you will import the data into.

<?php

//  $c_colslist is an array of name/value pairs. The name matches a name in the CSV, 
//  while the value is the name of the column in the table where the value is stored.
//  If there are changes in the CSV, you only have to edit this array and the table staging.
//  If the change is only a change in the order of the fields, you do not have to edit anything.
$c_colslist = array(
    'orderNumber' => 'orderNumber',
    'place' => 'place',
    'workOrderNum' => 'workOrderNum',
    'Column1' => 'userName'
);


//  $csvCols is an array containing the list of column names found in the CSV file, in the order they are found.
$csvCols = array();

//  

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


//  $rowIDs will contain a list of the unique IDs of all inserted rows.
$rowIDs = array();

$rowno = 0;
while(!feof($handle)){
    //  Get the row of data.
    //  If 1st row, it has the column names
    $rowno++;
    $row = fgetcsv($handle, 0, ",");
    if($rowno == 1) {
        //  Save the column name list
        $csvCols = $row;
        //  Pre-create the insert statement (all but the data)
        $qstr = "";
        $qstr .= "INSERT INTO `staging` (";
        $cols = array();
        //  $col_idx contains a list of indexes for each CSV column name (ie. the order they are found in the file)
        $col_idx = array(0);
        $colno = 0;
        foreach($csvCols as $idx => $c_colname) {
            $cols[$colno] = "`".$c_colslist[$c_colname]."`"
            $col_idx[$c_colname] = $colno;
            $colno++;
        }
        $qstr .= implode(',',$cols). ") ";
    } else {
        //  Build the INSERT statement
        $mqstr = $qstr; //  Start with the query string created when the first row was read.
        $mqstr .= ") VALUES (";
        //  Get the values in the same order as the columns.
        $vals = array();
        foreach($csvCols as $idx => $c_colname) {
            $val[] = "'".$row[$col_idx[$c_colname]]."'";
        }
        $mqstr .= implode(',',$vals);
        $mqstr .= ");";
        $query = mysqli_query($connect, $mqstr);
        $rowIDs[] = mysqli_insert_id($connect);
    }

}

//  Now we can copy the records into the other tables.

/*INSERT INTO CLIENTS TABLE FROM STAGING TABLE*/
$q2 .= "INSERT INTO clients (`orderNumber`,`place`,`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`address2`,`servicePreformed`)";
$q2 .= " SELECT `orderNumber`,`place`,`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`address2`,`servicePreformed`";
$q2 .= " FROM `staging`";
$q2 .= " WHERE `StageID` IN (";
$q2 .= implode(',',$rowIDs);
$q2 .=  ");";
$query = mysqli_query($connect, $q2);

/*INSERT INTO METERS TABLE FROM STAGING TABLE*/
$q3 .= "INSERT INTO meters (workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName`,`bypassSize`,`meterSize`,`meterType`,`manufacturer`,`registration`,`technician`,`linePressurePSI`,`lat`,`lon`,`lowSideRrBefore`,`highSideRrBefore`,`firesideRrBefore`,`lowSideRrAfter`,`highSideRrAfter`,`firesideRrAfter`,`vgOxygen`,`vgCombustGas`,`vgCarbonMon`,`vgHydroSulf)";
$q3 .= " SELECT workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName`,`bypassSize`,`meterSize`,`meterType`,`manufacturer`,`registration`,`technician`,`linePressurePSI`,`lat`,`lon`,`lowSideRrBefore`,`highSideRrBefore`,`firesideRrBefore`,`lowSideRrAfter`,`highSideRrAfter`,`firesideRrAfter`,`vgOxygen`,`vgCombustGas`,`vgCarbonMon`,`vgHydroSulf ";
$q3 .= " FROM staging";
$q3 .= " WHERE `StageID` IN (";
$q3 .= implode(',',$rowIDs);
$q3 .=  ");";
$query = mysqli_query($connect, $q3);

/*INSERT INTO TESTS TABLE FROM STAGING TABLE*/
$q4 .= "INSERT INTO `tests` (`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`test1TestRateGPM`,`test1MeterVol`,`test1TesterVol`,`test1Accuracy`,`test1CorrectAcc`,`test2TestRateGPM`,`test2MeterVol`,`test2TesterVol`,`test2Accuracy`,`test2CorrectAcc`,`test3TestRateGPM`,`test3MeterVol`,`test3TesterVol`,`test3Accuracy`,`test3CorrectAcc`,`test4TestRateGPM`,`test4MeterVol`,`test4TesterVol`,`test4Accuracy`,`test4CorrectAcc`,`test5TestRateGPM`,`test5MeterVol`,`test5TesterVol`,`test5Accuracy`,`test5CorrectAcc`,`test6TestRateGPM`,`test6MeterVol`,`test6TesterVol`,`test6Accuracy`,`test6CorrectAcc`,`test7TestRateGPM`,`test7MeterVol`,`test7TesterVol`,`test7Accuracy`,`test7CorrectAcc`,`test8TestRateGPM`,`test8MeterVol`,`test8TesterVol`,`test8Accuracy`,`test8CorrectAcc`,`inletValveSize`,`InletValveType`,`inletValveCond`,`outletValveSize`,`outletValveType`,`outletValveCond`,`bypassValveSize`,`bypassValveType`,`bypassValveCond`,`vaultLength`,`vaultWidth`,`vaultHeight`,`meterLocation`,`testPort`,`testPortInstalled`,`testPortSize`,`picture`,`comments`,`testResults`,`retest`,`test1TestRateGPM2`,`test1MeterVol2`,`test1TesterVol2`,`test1Accuracy2`,`test1CorrectAcc2`,`test2TestRateGPM2`,`test2MeterVol2`,`test2TesterVol2`,`test2Accuracy2`,`test2CorrectAcc2`,`test3TestRateGPM2`,`test3MeterVol2`,`test3TesterVol2`,`test3Accuracy2`,`test3CorrectAcc2`,`test4TestRateGPM2`,`test4MeterVol2`,`test4TesterVol2`,`test4Accuracy2`,`test4CorrectAcc2`,`test5TestRateGPM2`,`test5MeterVol2`,`test5TesterVol2`,`test5Accuracy2`,`test5CorrectAcc2`,`test6TestRateGPM2`,`test6MeterVol2`,`test6TesterVol2`,`test6Accuracy2`,`test6CorrectAcc2`,`test7TestRateGPM2`,`test7MeterVol2`,`test7TesterVol2`,`test7Accuracy2`,`test7CorrectAcc2`,`test8TestRateGPM2`,`test8MeterVol2`,`test8TesterVol2`,`test8Accuracy2`,`test8CorrectAcc2`) ";
$q4 .= "SELECT `workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`date`,`utility`,`serialNumber`,`serviceName`,`test1TestRateGPM`,`test1MeterVol`,`test1TesterVol`,`test1Accuracy`,`test1CorrectAcc`,`test2TestRateGPM`,`test2MeterVol`,`test2TesterVol`,`test2Accuracy`,`test2CorrectAcc`,`test3TestRateGPM`,`test3MeterVol`,`test3TesterVol`,`test3Accuracy`,`test3CorrectAcc`,`test4TestRateGPM`,`test4MeterVol`,`test4TesterVol`,`test4Accuracy`,`test4CorrectAcc`,`test5TestRateGPM`,`test5MeterVol`,`test5TesterVol`,`test5Accuracy`,`test5CorrectAcc`,`test6TestRateGPM`,`test6MeterVol`,`test6TesterVol`,`test6Accuracy`,`test6CorrectAcc`,`test7TestRateGPM`,`test7MeterVol`,`test7TesterVol`,`test7Accuracy`,`test7CorrectAcc`,`test8TestRateGPM`,`test8MeterVol`,`test8TesterVol`,`test8Accuracy`,`test8CorrectAcc`,`inletValveSize`,`InletValveType`,`inletValveCond`,`outletValveSize`,`outletValveType`,`outletValveCond`,`bypassValveSize`,`bypassValveType`,`bypassValveCond`,`vaultLength`,`vaultWidth`,`vaultHeight`,`meterLocation`,`testPort`,`testPortInstalled`,`testPortSize`,`picture`,`comments`,`testResults`,`retest`,`test1TestRateGPM2`,`test1MeterVol2`,`test1TesterVol2`,`test1Accuracy2`,`test1CorrectAcc2`,`test2TestRateGPM2`,`test2MeterVol2`,`test2TesterVol2`,`test2Accuracy2`,`test2CorrectAcc2`,`test3TestRateGPM2`,`test3MeterVol2`,`test3TesterVol2`,`test3Accuracy2`,`test3CorrectAcc2`,`test4TestRateGPM2`,`test4MeterVol2`,`test4TesterVol2`,`test4Accuracy2`,`test4CorrectAcc2`,`test5TestRateGPM2`,`test5MeterVol2`,`test5TesterVol2`,`test5Accuracy2`,`test5CorrectAcc2`,`test6TestRateGPM2`,`test6MeterVol2`,`test6TesterVol2`,`test6Accuracy2`,`test6CorrectAcc2`,`test7TestRateGPM2`,`test7MeterVol2`,`test7TesterVol2`,`test7Accuracy2`,`test7CorrectAcc2`,`test8TestRateGPM2`,`test8MeterVol2`,`test8TesterVol2`,`test8Accuracy2`,`test8CorrectAcc2` ";
$q4 .= "FROM `staging` ";
$q4 .= " WHERE `StageID` IN (";
$q4 .= implode(',',$rowIDs);
$q4 .=  ");";
$query = mysqli_query($connect, $q4);

/*INSERT INTO COSTS TABLE FROM STAGING TABLE*/
$q5 .= "INSERT INTO `costs` (`workOrderNum`,`onsiteSurveyTestCost`,`onsiteSurveyTestRepairCost`,`offsiteSurveyTestCost`,`offsiteSurveyTestRepairCost`,`onsiteTestOnlyCost`,`onsiteTestRepairOnlyCost`,`onsiteRepairOnly`,`testPort2`,`repairCompleteMeterReplacement`,`repairCompleteMeterReplacementLaborCost`,`umeCost`,`umeLaborCost`,`rotatingLowSideDiskChamber`,`rotatingLowSideDiskChamberLaborCost`,`turbineChamberCost`,`turbineChamberLaborCost`,`automaticValveCost`,`automaticValveLaborCost`,`strainerCost`,`strainerLaborCost`,`lowRegisterCost`,`lowRegisterLaborCost`,`highRegisterCost`,`highRegisterLaborCost`,`miuCost`,`miuLaborCost`,`totalCost`) ";
$q5 .= "SELECT `workOrderNum`,`onsiteSurveyTestCost`,`onsiteSurveyTestRepairCost`,`offsiteSurveyTestCost`,`offsiteSurveyTestRepairCost`,`onsiteTestOnlyCost`,`onsiteTestRepairOnlyCost`,`onsiteRepairOnly`,`testPort2`,`repairCompleteMeterReplacement`,`repairCompleteMeterReplacementLaborCost`,`umeCost`,`umeLaborCost`,`rotatingLowSideDiskChamber`,`rotatingLowSideDiskChamberLaborCost`,`turbineChamberCost`,`turbineChamberLaborCost`,`automaticValveCost`,`automaticValveLaborCost`,`strainerCost`,`strainerLaborCost`,`lowRegisterCost`,`lowRegisterLaborCost`,`highRegisterCost`,`highRegisterLaborCost`,`miuCost`,`miuLaborCost`,`totalCost ` ";
$q5 .= "FROM `staging` ";
$q5 .= " WHERE `StageID` IN (";
$q5 .= implode(',',$rowIDs);
$q5 .=  ");";
$query = mysqli_query($connect, $q5);

/*INSERT INTO WORKORDERS TABLE FROM STAGING TABLE*/
$q6 .= "INSERT INTO `workorders` (`workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName`) ";
$q6 .= "SELECT `workOrderNum`,`lowSideMIUNum`,`highSideMIUNum`,`accountNum`,`custName`,`address`,`locID`,`utility`,`serialNumber`,`serviceName  ";
$q6 .= "FROM `staging` ";
$q6 .= " WHERE `StageID` IN (";
$q6 .= implode(',',$rowIDs);
$q6 .=  ");";
$query = mysqli_query($connect, $q6);

?>
Sign up to request clarification or add additional context in comments.

6 Comments

I've worked this into my code, essentially just wrapping it in my 'if(asset($_POST['submit'])' call but I'm getting an error for an unexpected (T_Variable) at this line: $col_idx[$c_colname] = $colno; I actually just fixed it by adding a semi colon to the line above it: $cols[$colno] = "".$c_colslist[$c_colname].""; This fixed the issue, but now I'm getting an undefined index for every record in the CSV occurring at that line. Any idea what might be happening on the $cols[$colno] line?
I think I see a typo. Change the line $col_idx = array(0); to $col_idx = array(); (About line 40)
Ok, I removed that '0' and Added some debug statements. No SQL errors on the web page, just the undefined index for the PHP console. But on my debug dump page it shows the insert for the staging table as (,,,,,,,,,,,) so it seems to not be catching the variables for some reason
yes, undefined index (and the first few elements are undefined offset) all on this line: $cols[$colno] = "".$c_colslist[$c_colname].""; I'm trying a few things but no luck yet
Add a line after the foreach loop where $col_idx is built: echo "<p>col_idx:<pre>".print_r($col_idx,true)."</pre></p>"; and see if it is creating the array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.