3

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. 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 !

  • You may want to read [this](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) about SQL injection. – Joachim Isaksson Jan 22 '16 at 06:04
  • 1
    Inserting a date into mysql is via a String that is formatted correctly: YYYY-MM-DD. Check your insert to make sure what you're trying to insert is actually formatted correctly. – Lance Jan 22 '16 at 06:05
  • Additionally, since you're working on your mysql code, be sure to use mysqli instead of mysql libraries, since the latter is depreciated. – Lance Jan 22 '16 at 06:07
  • @JoachimIsaksson Thanks for the info but im just using this code for own as a tool for my need. When I have a big project I will use ur suggestion again thanks ! – Carlvic Japitana Lim Jan 22 '16 at 07:05

3 Answers3

2

to insert date or timestamp into database you need to create date object. use this

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

Gagan Upadhyay
  • 255
  • 2
  • 11
  • @lance I know it store as string into database...but if date formate is not match with database date format then it saves 0000-000-00 you have to use date('y-m-d') to make formate correct. – Gagan Upadhyay Jan 22 '16 at 06:09
  • Thanks for idea but i use '".date('y-m-d', strtotime($data[24]))."' to fix my code instead of closed_on – Carlvic Japitana Lim Jan 22 '16 at 07:07
2

You need to pass the string now to strtotime()

This will returns current time in UNIX format.

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.

2) Enclosing function parameters with backtick is parse error. Parameters should be enclosed with single quotes.

Pupil
  • 23,834
  • 6
  • 44
  • 66
2

use this

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