doudiyu1639 2017-06-19 03:55
浏览 34

通过使用PHP导入.csv文件在表中存在日期时的SQL Update查询

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;
  • 写回答

2条回答 默认 最新

  • douweiduo7526 2017-06-19 04:08
    关注

    mysql accepts date format in 'yyyy-mm-dd' so your query should be $sqlgetdate = ("SELECT DATE from $table where DATE = '2017-03-05' "); and $sqlupdate = ("UPDATE $table SET Fieldname = 'John' where DATE = '2017-03-05' ");

    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件