美好的一天,
我尝试使用PHP和mysql脚本将数据导入我的数据库但是在日期列插入as 0000-00-00而不是它的实际日期.请检查下面的代码
if (isset($_POST['submit'])) {
$i=0;
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
}
date('Y-m-d', strtotime(`Closed On`));
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($i>0) {
$import="INSERT into ".$user."_products (`Invoice No`,`Receipt No`,`Category`,`Sub Category`,`ProductName`,`Sold On`,`Guest`,`GuestCenter`,
`Employee Code`,`SoldBy`,`Quantity`,`Promotion`,`Price`,`discount`,`Prepaidcard Redemption`,`Net Price`,`Tax`,`ShippingCharge`,
`Package Redemption`,`Price Paid`,`Sale Value`,`Payment Type`,`Createdby`,`Status`,`Closed On`,`GuestCode`,`FirstVisit`,`Member`,`ClosedBy`,`BusinessUnit`)
values('$data[0]','$data[1]','$data[2]','$data[3]','".MysqL_real_escape_string($data[4])."','$data[5]','".MysqL_real_escape_string($data[6])."','$data[7]','$data[8]','$data[9]','$data[10]',
'$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]',
'$data[22]','$data[23]','$data[24]','$data[25]','$data[26]','$data[27]','$data[28]','$data[29]')";
MysqL_query($import) or die(MysqL_error());
}
$i++;
}
谢谢 !
解决方法:
要将日期或时间戳插入数据库,您需要创建日期对象.
用这个
date(‘y-m-d’,strtotime($data [‘closed_on’]))