Jump to content

Recommended Posts

Hi,

 

I'm trying to create a script that allow me to update my product quantity from external csv file. I've done successfully the first part, update db.

 

But now i want to send one mail with results that are not in db. I check DB from ean, and update product quantity by ean. Sometimes, the csv file have some ean that are not in db, i want to receive one e-mail with this ean's.

 

Here is my code:

 

<?php // Connect to MySQL mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); #if first row of csv file is headings set $row to 1. $row = 1; #database primary table $table_to_update = "table_name"; #get the csv file $handle = fopen("http://www.csvfilewebsite.com/file.csv", "r"); #go through the csv file and print each row with fields to the screen. #and import them into the database updating only the price and quantity  while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) {     $num = count($data);     echo "<p> $num fields in line $row: <br /></p>\n";     $row++;     for ($c=0; $c < $num; $c++) {         if ($c = 1) {         $ean13 = $data[($c - 1)];         echo $ean13 . " SKU Assigned <br />\n";         }         if ($c = 3) {         $quantity = $data[($c - 1)];         mysql_query("UPDATE $table_to_update SET quantity='$quantity' WHERE ean13='$ean13'")          or die(mysql_error());          echo $product_quantity . " Imported for row $row in product $ean13 <br />\n";         }         // would have to add an additional  if statement for each field being updated and know the order of the fields from your csv file         //echo $data[$c] . "Imported <br />\n";                       } } fclose($handle); echo "<h1>Update Complete.</h1>"; ?> 

 

 

Now, how to check csv ean's that aren't in db, and send mail with this ean's?

 

Hope someone can help me.

 

Regards

 

Mozack

Link to comment
https://forums.phpfreaks.com/topic/215358-update-db-from-csv-and-send-mail/
Share on other sites

this should help you to figure out the ean's that aren't in db

http://php.net/manual/en/function.mysql-affected-rows.php

 

only problem with this could be:

 

When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.

 

evaluate if  the last is a certain possibility (quantity = '$quantity') before decide to use mysql_affected_rows()... your alternatives then could be:

- Do a select for each element in your file before the update (I don't like this) or

- Store your file ean's in an array

- Use the generated array to construct an INSERT and insert those file ean's in a temporary table (memory) and JOIN it with your real table to obtain the missing ean's... (means 1 INSERT and 1 SELECT no multiples as in the first case)

- generate the mail from this result.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.