I wrote a php script to import a CSV file into a wordpress database. I have been working through lots of different errors today which were crashing the website till I fixed them.
Now all the errors are cleared and the string at the end of the function is being printed to the screen as if everything is ok but it is not entering my data to the DB.
Also for testing sake I removed the create DB part and only left the remove to see if it would remove the DB and it did not. So basically it appears my function does not have errors but it is being ran. Also it is not actually doing anything. I do not understand how it could run through the function telling me each time there is an error or a file path that does not exist but not actually carry out the operations I am trying to execute.
Any input on how to troubleshoot this or where to go from here would be appreciated,
function productsExec() {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
global $wpdb;
global $simple_prod_version;
$table_name = $wpdb->prefix . "wuno_inventory";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "DROP TABLE IF EXISTS $table_name";
dbDelta($sql);
// $wpdb->query($sql);
$sql = "CREATE TABLE " . $table_name . " (
id int(8) NOT NULL AUTO_INCREMENT,
wuno-product varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
wuno-description varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
wuno-alternates varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
wuno-onhand varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
wuno-condition varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
dbDelta($sql);
// $wpdb->query($sql);
//Upload File
if (is_uploaded_file($_FILES['inventory.csv']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['inventory.csv']['name'] ." uploaded successfully." . "</h1>";
echo "<h2>Displaying contents:</h2>";
readfile($_FILES['inventory.csv']['tmp_name']);
}
//Import uploaded file to Database
$handle = fopen(inventory.csv, 'w');
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
$insert = "INSERT INTO" . $table_name . "(wuno-product, wuno-description, wuno-alternates, wuno-onhand, wuno-condition)" .
"VALUES ('$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]')";
$results = $wpdb->query( $insert );
}
fclose($handle);
echo 'Everything seems to be running smoothly.';
}
}