简体   繁体   English

SQL插入日期始终显示为NULL

[英]SQL Insert Date Always comes out NULL

I'm trying to use an upload page to insert into my database with the following code: 我正在尝试使用一个上传页面通过以下代码插入我的数据库:

if($file!=="")
    {

        echo "<br/>".$file;
        $handle = fopen($file, "r");
        $row = 0;

        $delete_records = mysql_query("DELETE FROM affiliationagreements");
        $delete_records = mysql_query("DELETE FROM college");
        $delete_records = mysql_query("DELETE FROM program");
        $delete_records = mysql_query("DELETE FROM facility");
        $delete_records = mysql_query("DELETE FROM submitor");
        $delete_records = mysql_query("DELETE FROM location");
        //will loop each record in the CSV file
        while(($fileop = fgetcsv($handle,1000,",")) !== false )
        {
            //columns names of the CSV file are not needed
            if($row==0)
            {
                $row++;
            }
            else
                {
                    //accept apostrophes in the strings
                    $fileop = array_map("mysql_real_escape_string",$fileop);

                    $sql = mysql_query("INSERT INTO affiliationagreements(id, AANumber, Facility, Submitor, Program, Location, College, SubmissionDate, Action, EffectiveDate, Status, ExpirationDate)
                                    VALUES('',
                                    '$fileop[0]', 
                                    '$fileop[1]', 
                                    '$fileop[2]', 
                                    '$fileop[3]', 
                                    '$fileop[4]', 
                                    '$fileop[5]', 
                                    '$fileop[11]', 
                                    '$fileop[23]', 
                                    '$fileop[24]', 
                                    '$fileop[25]', 
                                    '$fileop[26]') 
                                ")or die(mysql_error());

To just give a sample, and when I upload my CSV file to add the values, I print them out in the console and see that the values are being read correctly and they are. 只是提供一个示例,当我上传CSV文件以添加值时,我在控制台中将其打印出来,并看到正确且正确地读取了这些值。 But, once my php script ends and I return to the main page, my dates are all null. 但是,一旦我的php脚本结束并且我返回主页,我的日期都为空。 None of them are the values what are reflected in the csv file. 它们都不是csv文件中反映的值。 Here is the schema for my database: 这是我的数据库的架构:

CREATE TABLE `affiliationagreements` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `AANumber` varchar(20) DEFAULT NULL,
  `Facility` varchar(150) DEFAULT NULL,
  `Submitor` varchar(50) DEFAULT NULL,
  `Program` varchar(60) DEFAULT NULL,
  `Location` varchar(50) DEFAULT NULL,
  `College` varchar(50) DEFAULT NULL,
  `SubmissionDate` date DEFAULT NULL,
  `Action` varchar(50) DEFAULT NULL,
  `EffectiveDate` date DEFAULT NULL,
  `Status` varchar(50) DEFAULT NULL,
  `ExpirationDate` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

If I change SubmissionDate, EffectiveDate, and ExpirationDate to a varchar, they insert correctly but I can't use varchar because I am comparing date values. 如果我将SubmissionDate,EffectiveDate和ExpirationDate更改为varchar,它们将正确插入,但由于我正在比较日期值,因此无法使用varchar。 And Advice? 和建议?

***Update. ***更新。 In the CSV file, the format is MM/DD/YYYY. 在CSV文件中,格式为MM / DD / YYYY。 I didn't think this would be a problem. 我认为这不会成为问题。 Would it be better to change this? 改变这个会更好吗? And I'm deleting records because my boss wanted the DB cleared before a file was reuploaded since the uploaded file was an update of the previously uploaded one. 我要删除记录,因为我的老板希望在重新上传文件之前清除数据库,因为上传的文件是先前上传的文件的更新。 **** ****

Check your date format, as MySql it needs to be in YYYY-MM-DD format 检查您的日期格式,因为MySql必须为YYYY-MM-DD格式

INSERT INTO table SET date = '2014-05-13'

If you have different format, the date will store '0000-00-00' instead. 如果您使用其他格式,则日期将改为存储“ 0000-00-00”。 So double check your insertion by echo your query string before running the query. 因此,在运行查询之前,通过回显查询字符串来仔细检查您的插入。

mysql date field only accepts dates in this format: 0000-00-00 mysql date字段仅接受以下格式的日期: 0000-00-00

You can use DATE_FORMAT() in your insert query but I can't give exaples becuase you didn't post what format your actual date is. 您可以在插入查询中使用DATE_FORMAT() ,但由于您未发布实际日期的格式,因此我无法给出确切的答案。

http://www.w3schools.com/sql/func_date_format.asp http://www.w3schools.com/sql/func_date_format.asp

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

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