I'm trying for day to get the following code to work. When running it no SQL or PHP errors occur and it just runs smoothly.
BUT neither a new record is inserted nor old records are updated.
The basic function is reading a csv file and importing it into a MYSQL database.
<?php
//DB connect
$db = mysqli_connect("127.0.0.1", "adm", "???", "ip-miniapps");
//DB connection check
if (!$db) {
$txt = "
update_cron.php - ".date("y-m-d");
$txt .= "
Error: Can't connect to MySQL." . PHP_EOL;
$txt .= "
Debug-Fehlernummer: " . mysqli_connect_errno() . PHP_EOL;
$txt .= "
Debug-Fehlermeldung: " . mysqli_connect_error() . PHP_EOL;
$myfile = file_put_contents('/www/website_ip-miniapps/scripts/executables/update_cron.log', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
exit;
}
//START - update routine for table app_all_supplier_master_data
if (($handle = fopen("/www/website_ip-miniapps/interfaces/SAP/supplier_md.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
//remove excess field
while(count($data)>13)
{
array_pop($data);
}
//mysqli-real_escape_string
$i=0;
while($i<=12)
{
$data[$i]=mysqli_real_escape_string($db, $data[$i]);
$i++;
}
$line = implode("','", $data);
$query = "INSERT INTO `app_all_supplier_master_data` (`supplier_code`,`name_1`,`name_2`,`street`,`zip`,`city`,`country`,`ekorg`,`supplier_node`,`node_name`,`classification`,`gbc`,`gbc_name`) VALUES('";
$query .= $line;
$query .= "') ON DUPLICATE KEY UPDATE
`supplier_code`='{$data[0]}',
`name_1`='{$data[1]}',
`name_2`='{$data[2]}',
`street`='{$data[3]}',
`zip`='{$data[4]}',
`city`='{$data[5]}',
`country`='{$data[6]}',
`ekorg`='{$data[7]}',
`supplier_node`='{$data[8]}',
`node_name`='{$data[9]}',
`classification`='{$data[10]}',
`gbc`='{$data[11]}',
`gbc_name`='{$data[12]}'";
mysqli_query($db, $query) or die(mysqli_error($db));
}
fclose($handle);
}
//END - update routine for table app_all_supplier_master_data
//DB close
mysqli_close($db);
$txt = "
update_cron.php - ".date("y-m-d")." - Success";
$myfile = file_put_contents('/www/website_ip-miniapps/scripts/executables/update_cron.log', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
?>
I echoed $query, but can't see anything.
INSERT INTO `app_all_supplier_master_data`
(`supplier_code`,`name_1`,`name_2`,`street`,`zip`,`city`,
`country`,`ekorg`,`supplier_node`,`node_name`,`classification`,
`gbc`,`gbc_name`)
VALUES('Vendor','Name 1','Name 2','Street','PostalCode','City',
'Cty','POrg','Hi.lv.hier',
'Vendor number of higher-level','Preisausz.','PGr',
'Description')
ON DUPLICATE KEY UPDATE `supplier_code`='Vendor',
`name_1`='Name 1',
`name_2`='Name 2',
`street`='Street',
`zip`='PostalCode',
`city`='City',
`country`='Cty',
`ekorg`='POrg',
`supplier_node`='Hi.lv.hier',
`node_name`='Vendor number of higher-level',
`classification`='Preisausz.',
`gbc`='PGr',
`gbc_name`='Description'
I solved it myself thank you for the support. I compared the raw data from the CSV file to the Structure of the DB table.
The problem was the data I wanted to add was formatted wrong for example.
My key column is supplier_code (VARCHAR(6)) but the data I tried to add was "0000400000"
So I simply trimmed of the excess zeros and it worked.