New Zenner
- Join Date:
- May 2014
- Location:
- Spokane Washington
- Posts:
- 13
- Plugin Contributions:
- 0
Custom Product add routine fails to find recorc after $db->execute
This is a simplified version of code that read through a csv file and adds manufactures and product to the database. It is suppose to only add manufactures that do not alread exist in the table, however it can't find a duplicate record that was just added previously in this sequence. I don't understand why after successfully inserting a record it can't find it when it reads the next record that has the same manufacturers name and thereby skipping the insert of a duplicate record. In the actual program I use the zen_db_perform but I did this way as it is a little easier to debug.
I would appreciate any input you could give.
HERE IS THE CODE <<<<<<<<<<
while (($data = fgetcsv($handle, 0, " ",chr(0))) !== FALSE) {
echo chr(13).$data[$manufacturer_sn].chr(13);
$mfsql = "SELECT count(*) as total, manufacturers_id, manufacturers_name FROM " . TABLE_MANUFACTURERS . " WHERE manufacturers_name=\"" . $data[$manufacturer_sn] . "\"";
echo $mfsql.chr(13);
$manufacturers = $db->Execute($mfsql);
echo 'counts '.$manufacturers->fields['total'].chr(13);
echo 'search name '.$data[$manufacturer_sn].chr(13);
echo 'id '.$manufacturers->fields['manufacturers_id'].chr(13);
echo 'name '.$manufacturers->fields['manufacturers_name'].chr(13);
if ($manufacturers->fields['manufacturers_name'] != $data[$manufacturer_sn]) {
echo 'No Match'.chr(13);
$today = date("Y-m-d G:i:s");
$query = "INSERT INTO manufacturers (manufacturers_name, date_added) VALUES (\"".$data[$manufacturer_sn]."\",\"".$today."\")";
echo '$query '.$query.chr(13);
$db->Execute($query);
$manufacturers_id = $db->insert_ID();
echo $manufacturers_id.chr(13);
}
}
RESULTS OF ABOVE EXECUTED CODE <<<<<<<
****** This first entry is correct as there is no existing manfacturer record for "STANLEY TOOLS". *****
STANLEY TOOLS
SELECT count(*) as total, manufacturers_id, manufacturers_name FROM manufacturers WHERE manufacturers_name="STANLEY TOOLS"
counts 0
search name STANLEY TOOLS
id
name
No Match
$query INSERT INTO manufacturers (manufacturers_name, date_added) VALUES ("STANLEY TOOLS","2016-01-02 17:13:29")
4
****** These next 2 records are incorrect as it just sucessfully inserted the same manufacturer previously. *******
STANLEY TOOLS
SELECT count(*) as total, manufacturers_id, manufacturers_name FROM manufacturers WHERE manufacturers_name="STANLEY TOOLS"
counts 0
search name STANLEY TOOLS
id
name
No Match
$query INSERT INTO manufacturers (manufacturers_name, date_added) VALUES ("STANLEY TOOLS","2016-01-02 17:13:29")
5
STANLEY TOOLS
SELECT count(*) as total, manufacturers_id, manufacturers_name FROM manufacturers WHERE manufacturers_name="STANLEY TOOLS"
counts 0
search name STANLEY TOOLS
id
name
No Match
$query INSERT INTO manufacturers (manufacturers_name, date_added) VALUES ("STANLEY TOOLS","2016-01-02 17:13:29")
6