I have a code that will get to create and insert .csv files to MySQL. Now what I want to get is how to Update my data if there is already an existing date in my table by importing a .csv file.
I tried to do it with a simple Update Query and its working fine... but the tricky part is to get the date from .csv file and compare it to the date that is in MySQL. Can you give me any hints on how to this?
Here's my code:
$file = 'C:\Users\HP\Desktop\csvfiles\ADJTIME.csv';
$table = 'adjtime';
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen($file,'r');
// first row, structure
if ( ($data = fgetcsv($handle) ) === FALSE ) {
//echo "Cannot read from csv $file";die();
}
$fields = array();
$field_count = 0;
for($i=0;$i<count($data); $i++) {
$f = strtolower(trim($data[$i]));
if ($f) {
// normalize the field name, strip to 20 chars if too long
$f = substr(preg_replace ('/[^0-9a-z]/', '_', $f), 0, 100);
$field_count++;
if($f == 'date'){
$fields[] = '`'.$f.'` DATE()';
} else {
$fields[] = '`'.$f.'` VARCHAR(500)';
}
}
}
$sqlgetdate = ("SELECT DATE from $table where DATE = '5/3/2017' ");
echo $sqlgetdate;
$sqlupdate = ("UPDATE $table SET Fieldname = 'John' where DATE = '5/3/2017' ");
echo $sqlupdate;