0

In my plugin the user provides some CSV files that are processed and either inserted or updated based on a sku (which acts as a foreign key to my DB and a primary key for where the CSV comes from).

I process each line and either insert product info or updated based on if the sku is available. There are 5503 products total. I have two tables one to hold the product id and sku and another to hold the details for that product.

when I run my function I check to see if the sku has a match. If not, I insert into the tables, if it does, I update. The first time I run, obviously everything should be an insert. I've done some debugging and found that insert is run every time, meaning there's not an error causing updated to run.

I've ran print_r on the prod_details insert and I get all 5503 sets of arrays so nothing is being skipped. The products table is correct but for some reason the prod_details table doesn't get all the inserts, only about 4122 are inserted of the 5503. I'm not sure if I'm dealing with a timeout, maybe a synchronous issue.

I've compared the skus between the two tables and found large chunks missing.

$prodTable = $wpdb->prefix.'products';
$prodDetails = $wpdb->prefix.'prod_details';

$skuId = $wpdb->get_var("SELECT id FROM $prodTable WHERE sku=$sku");

if($skuId) {
    //update the product table
    $wpdb->update(
    $prodTable,
    array(
        'status' => $status
    ),
    array(
        'id' => $skuId
    )
    );

    $wpdb->update(
    $prodDetails,
    array(
        'prodName' => $prodName,
        'typeName' => $prodType,
        'siteTypeName' => $siteType,
        'subTypeName' => $prodSubtype,
        'prodProducer' => $prodProducer,
        'styleName' => $prodStyle,
        'supplierName' => $supplier,
        'prodVintage' => $prodVintage
    ),
    array(
        'sku' => $sku
    )
    );

} else {
    $wpdb->insert(
    $prodTable,
    array(
        'sku' => $sku,
        'status' => $status
    )
    );
    $wpdb->insert(
    $prodDetails,
    array(
        'sku' => $sku,
        'prodName' => $prodName,
        'typeName' => $prodType,
        'siteTypeName' => $siteType,
        'subTypeName' => $prodSubtype,
        'prodProducer' => $prodProducer,
        'styleName' => $prodStyle,
        'supplierName' => $supplier,
        'prodVintage' => $prodVintage
    )
    );
}

I understand there's some normalization I could probably do here but that would explain the insert issue. The above loops for each line of the CSV file (5503 times).

My local database inserts everything fine.

  • I would start with checking if there are errors don't remeber of the top of my head but the class should have something, and probably the insert should return error indication if it fails – Mark Kaplun Feb 3 '16 at 18:38
  • Run some more debugging and it turns out it was character encoding. Some of the characters are spanish characters. I can solve it by running mb_convert_encoding($str,'UTF-8') but that means the characters stored are not wrong... – dcp3450 Feb 3 '16 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.