2

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.';
    }
}
wuno
  • 9,547
  • 19
  • 96
  • 180
  • far as I can tell, this `INSERT INTO" . $table_name . "` translates to, and for example `INSERT INTOtablename` and you have hyphens for column names that MySQL is translating as `wuno MINUS product` etc. Checking for errors would have told you about it. – Funk Forty Niner Feb 11 '16 at 00:53
  • well I appreciate your feedback I am not getting any errors. I will try and figure out why – wuno Feb 11 '16 at 00:55
  • you sure error checking's on? have a look at this Q&A on WordPress Development Stack Exchange http://wordpress.stackexchange.com/questions/16382/showing-errors-with-wpdb-update - and `$wpdb->last_error` – Funk Forty Niner Feb 11 '16 at 00:58
  • Yes sir, it is on. Its displaying the typical errors that always show when error checking is on. I changed the wuno-products to wuno_products and I made sure the prefix was on the database table to be like everything else. Cause I am declaring that in table name. Only thing that has changed is my string at the end is not firing now but still no error. Ill check the link thank you. – wuno Feb 11 '16 at 01:01
  • you're welcome. You'll need to do the same thing for the other column names also. Good luck. – Funk Forty Niner Feb 11 '16 at 01:01
  • @Fred I think I have something now. Now I am getting a cache limiter already sent error. So it looks like you're 100 percent right! Would you mind answering my question with the correct sql format? – wuno Feb 11 '16 at 01:04
  • I made an edit to my answer in regards to `$handle = fopen(inventory.csv, 'w');` there should be quotes around the filename. As per the manual http://php.net/manual/en/function.fopen.php – Funk Forty Niner Feb 11 '16 at 01:19
  • [here is my solution you can check that from here ](https://stackoverflow.com/questions/47407711/read-csv-file-with-wordpress/60668905#60668905) – Darkcoder Mar 13 '20 at 10:41

1 Answers1

1

As mentioned in comments, and as per OP's request:

INSERT INTO" . $table_name . " translates to, and for example INSERT INTOtablename since there is no space between INFO and "

Plus, you have hyphens for column names that MySQL is translating as wuno MINUS product, as a math equation and the same for the other column names.

Consult http://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html on Identifier Qualifiers

You can use hyphens, but ticks are required to be used around the column names.

$insert = "INSERT INTO " . $table_name . " (`wuno-product`, `wuno-description`, `wuno-alternates`, `wuno-onhand`, `wuno-condition`)

Edit:

I spotted something else.

$handle = fopen(inventory.csv, 'w');

There should be quotes around the filename:

$handle = fopen("inventory.csv", 'w');

as per the manual on fopen() http://php.net/manual/en/function.fopen.php

$handle = fopen("c:\\folder\\resource.txt", "r");

and error reporting would have thrown you a notice about it.

Plus, since you're using $_FILES, and if using a form, it should be a POST method and contain a valid enctype.

However, this seems invalid $_FILES['inventory.csv'] or is unclear.

Usually, the first array parameter is a matching name attribute. Consult the manual link just above.

Also add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hey thank you very much for taking the time to help me. You really helped a lot. I am getting further along because you got me over that hump. Have a great night Fred. – wuno Feb 11 '16 at 01:20
  • @wuno You're most welcome. Please reload my answer again though, as I spotted yet something else in regards to `$_FILES['inventory.csv']` and have left a link to have a look into in regards to files handling. Have a great night too, thanks. *Cheers* – Funk Forty Niner Feb 11 '16 at 01:26
  • Ok thank you, It appears now the file upload is all i have left to fix. Basically I decided im just going to have them ftp the file to the server cause it could be as big as 88k lines in it. So I think I need to check if the file exist then convert it from csv to mysql. I am currently looking up if(isset) to see if that will do the trick. – wuno Feb 11 '16 at 01:31
  • @wuno You're welcome. `isset()` is ok, but it's best to use `!empty()` as in "not empty". As `isset()` could give a false positive for inputs. – Funk Forty Niner Feb 11 '16 at 01:33
  • !empty() vs file_exists(path) same thing or one is better than the other? – wuno Feb 11 '16 at 01:34
  • @wuno Oh, for `file_exists()`. Ok, that's a different thing. Well, I personally would first check if the uploaded file is for "not empty", then you can use `file_exists()` to check if it does exist and handle the errors on that, if you wish to overwrite an existing file if there is one, or you can also easily have it renamed. If you're handling files via a form, you can have a look at this answer http://stackoverflow.com/a/18705701/ which renames an uploaded file. – Funk Forty Niner Feb 11 '16 at 01:37
  • Ok great. Thanks again Fred. Have a great night bro. – wuno Feb 11 '16 at 01:40