PHP Load and update CSV file from dir
Following on from a previous question I posted, which I managed to get working great, I'm now stuck trying to check and store the date of the csv file and then execute the script if the csv file is newer. I plan on running a cron job when this is working properly in order to check regularly for a newer csv file. I just can't seem to be able to work out why the date and csv file aren't being updated into the database when I manually add a modified csv file to the directory and then run the script manually.
Any help greatly appreciated, S.
Current code:
$stat = stat('../data/filename.csv');
$datetime = date ("F d Y H:i:s.", $stat['mtime']);
$datetime = mysql_real_escape_string($datetime);
$datetime = strtotime($datetime);
$datetime = date('Y-m-d H:i:s',$datetime);
echo "<br />Last modified: " . $datetime;
$query=mysql_query("select * from iscsvtime");
$dateResult=mysql_fetch_array($query);
$stored = $dateResult['added'];
if (($stored <= $datetime) && ($handle = fopen("../data/filename.csv", "r")) !== FALSE) {
$i=0;
echo "<br />Stored date: " . $stored;
mysql_query("INSERT INTO iscsvtime(added) VALUES ('$datetime')");
mysql_query('TRUNCATE TABLE isstock;');
while (($data = fgetcsv($handle, 0, ",")) !== FALSE)
{
if($i>0){
$q="insert into isstock set feedid='".$data[0]."', vehicleid='".$data[1]."', registration='".$data[2]."', colour='".$data[3]."', fueltype='".$data[4]."', year='".$data[5]."', mileage='".$data[6]."', bodytype='".$data[7]."', doors='".$data[8]."', make='".$data开发者_如何学Go[9]."', model='".$data[10]."', variant='".$data[11]."', enginesize='".$data[12]."', price='".$data[13]."', previousprice='".$data[14]."', transmission='".$data[15]."', picturerefs='".$data[16]."', servicehistory='".$data[17]."', previousowners='".$data[18]."', description='".$data[19]."', fourwheeldrive='".$data[20]."', options='".$data[21]."', comments='".$data[22]."', new='".$data[23]."', used='".$data[24]."', site='".$data[25]."', origin='".$data[26]."', v5='".$data[27]."', condit='".$data[28]."', exdemo='".$data[29]."', franchiseapproved='".$data[30]."', tradeprice='".$data[31]."', tradepriceextra='".$data[32]."', servicehistorytext='".$data[33]."', capid='".$data[34]."'";
mysql_query($q);
}
?>
<tr>
<?php
foreach($data as $rec){
echo '<td>'.$rec.'</td>';
}
?>
</tr>
<?php
$i++;
}
}
fclose($handle);
EDIT: Turns out I managed to fix this myself; just had to change the if
statement slightly and change the INSERT INTO iscsvtime
to an UPDATE
. Updated code below:
if (($datetime > $stored) && ($handle = fopen("../data/filename.csv", "r")) !== FALSE) {
$i=0;
mysql_query("UPDATE iscsvtime SET added = '$datetime' WHERE id = '0'");
I'd be inclined to say that the mixing of single and double quotes is not helping your cause. This inconsistency may be leading to any number of possible errors, where it doesn't already impede readability.
Lines such as
echo "<br />Stored date: " . $stored;
could be replaced by
echo "<br />Stored date: {$stored}";
or use the same notation as later in the script
echo '<td>'.$rec.'</td>';
I believe this could be tripping up your SQL statement
mysql_query("INSERT INTO iscsvtime(added) VALUES ('$datetime')");
Try changing it to
mysql_query("INSERT INTO iscsvtime(added) VALUES ('{$datetime}')");
In addition to this, you could add some error logging by making use of the mysql error functions php.net/mysql_error, like so:
if(!mysql_query("INSERT INTO iscsvtime(added) VALUES ('{$datetime}')"))
{
echo mysql_errno . ': ' . mysql_error();
}
Other things to check include, correct location of file on disk as referenced in the functions that need it, is the file empty and finally, does the insert statement match the layout of the table, i.e. if the table has more than one column, are the non-provided columns set to allow NULL values, otherwise, these should be defined in the insert statement.
Hope this helps
精彩评论