CSV upload on front end of wordpress site (PHP/SQL

2019-03-05 12:27发布

问题:

I've made a lot of progress on this in some areas but struggling in others. Here's the objective: Existing wordpress site is being used by client. They want the admin user to access one of the front end pages with an upload option where they can upload a CSV (several daily). Then, upon accessing other pages in the portal, respective fields from the tables will be displayed (depending on the user). The CSV files have 201 fields, same order every time. In my php, I've setup a successful connection and coded 201 variables like so:

<?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']))
{

$coldata = array();

$coldata['orderNumber'] = $filesop[0];
$coldata['null'] = $filesop[1];
$coldata['workOrderNum'] = $filesop[2];
$coldata['lowSideMIUNum'] = $filesop[3];
$coldata['highSideMIUNum'] = $filesop[4];
$coldata['accountNum'] = $filesop[5];
$coldata['custName'] = $filesop[6];

Again, this line goes on through [200]. On the next portion, I will only paste certain variables to save space. This is where I index which tables certain variables will belong in.

$table_cols = array();

/*staging*/
$table_cols[0] ="null,orderNumber,null,workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum
/*clients*/
$table_cols[1] ="orderNumber,null,workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName
/*meters*/
$table_cols[2] ="workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address
/*tests*/
$table_cols[3] ="workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName,address
/*costs*/
$table_cols[4] ="workOrderNum,onsiteSurveyTestCost,onsiteSurveyTestRepairCost,offsiteSurveyTestCost
/*workorders*/
$table_cols[5] ="workOrderNum,lowSideMIUNum,highSideMIUNum,accountNum,custName

And now the SQL query:

$tablenames = array("staging","clients","meters","tests","costs","workorders");

for($tableno = 0;$tableno < sizeof($tablenames);$tableno++){
$q = "";
$q .= "INSERT INTO ".$tablenames[$tableno]." (".$table_cols[$tableno].") VALUES (";
$cols = explode("," ,$table_cols);
$data = array();
foreach($col as $key => $fldname) {
    $data[] = "'".$coldata[$fldname]."'";
}

$q .= implode(",",$data).");";

}
echo'File submitted';

When I run this, I get no PHP errors. I run it on Mamp, upload the CSV through an html submit form, it calls the php and then on my php index page I get my messages for successful connection and successful insertion. However, when I look in MySQL workbench and select from tables they are empty. My staging table was actually only created with one column for primary key but I don't know if this code will submit everything without established columns/fields in the database tables. In my table_cols array under the 'staging' option (index [0]), I actually have all 201 variables there, as the entire form will be housed in this one table just to be safe. Am I missing something here as to why it's not loading into the database?