我有一个csv文件,其中包含列:date_sourced, sha1, vsdt, trendx, notes
这些是csv中的一些示例:
DATE SOURCED, SHA1, VSDT, TRENDX, NOTES, MTF
11/9/2018,fffe455520ddc68e4f7a56a053efc78a208eb384,MS Office 1-0,Not Supported,test,
当我将csv插入mysql时,它的确正确,但是为什么我在mysql中的日期在使用以下代码更新后会返回0000-00-00:
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$date = $csvdata[0];
$sha1 = $csvdata[1];
$vsdt = $csvdata[2];
$trendx = $csvdata[3];
$notes = $csvdata[4];
// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_test WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}
// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_test` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_test (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}
$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
答案 0 :(得分:3)
因为您的表列类型是日期,请尝试使用strtotime更改日期格式
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_test` SET `date_sourced`='".date('Y-m-d',strtotime($date))."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_test (date_sourced,sha1,vsdt,trendx,notes) VALUES ('".date('Y-m-d',strtotime($date))."','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}