简体   繁体   English

插入日期列as 0000-00-00

[英]Insert into date column as 0000-00-00

Good day, 美好的一天,

I try to import data to my database using php and mysql script but on date column insert as 0000-00-00 instead of its actual date. 我尝试使用php和mysql脚本将数据导入我的数据库但是在日期列插入as 0000-00-00而不是它的实际日期。 Please check my code below 请检查下面的代码

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++;
}

Note: My date column is Closed On on my database. 注意:我的数据库上的日期列已关闭

Thanks ! 谢谢 !

to insert date or timestamp into database you need to create date object. 要将日期或时间戳插入数据库,您需要创建日期对象。 use this 用这个

date('ym-d', strtotime($data['closed_on'])) date('ym-d',strtotime($ data ['closed_on']))

You need to pass the string now to strtotime() now需要将字符串传递给strtotime()

This will returns current time in UNIX format. 这将以UNIX格式返回当前时间。

date('Y-m-d', strtotime('now'));

Your statement has two issues: 您的陈述有两个问题:

date('Y-m-d', strtotime(`Closed On`));

1) Closed On : does not evaluate to anything. 1) Closed On :不评估任何内容。

2) Enclosing function parameters with backtick is parse error. 2)用反引号括起函数参数是解析错误。 Parameters should be enclosed with single quotes. 参数应该用单引号括起来。

用这个

date('Y-m-d', strtotime($data[24]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM