dqfr90979 2015-07-02 00:25
浏览 57
已采纳

添加时间戳并将不成功的数据库条目添加到pdf

I've just started out programming in PHP & MYSQL. I'm wanting to create a CSV file importer that then writes entries to a database, but with various validations. I wanted to add a current time and date to the datetime column in which any products that are discontinued when the database says "Yes" will issue a date and time of the product being discontinued. I also want to add entries to a PDF file/ report format of various error conditions. The errors would include any entries that did not pass the if conditions such as regular expressions and were not added to the database.

<?php
    include_once('connection.php');
    date_default_timezone_set('Europe/London');
    $date = date('d/m/y h:i:s a', time());
    $filetxt = "./errors.txt";
    $var1 = 5;
    $var2 = 1000;
    $var3 = 10;
    if(isset($_POST["Import"]))
    {
        echo $filename=$_FILES["file"]["tmp_name"]; 
        if($_FILES["file"]["size"] > 0)
        {
            $file = fopen($filename, "r");
            while(($emapData = fgetcsv($file, 10000, ",")) !==FALSE)
            {
                // adds data to the sql database
                if($var1 <= $emapData[3] && $var3 <= $emapData[4] && $var2 >= $emapData[4] && preg_match("/^[a-zA-Z0-9]+$/", $value) == $emapData[1] && preg_match("[a-zA-Z]", $value) == $emapData[2] && preg_match("[a-zA-Z]", $value) == $emapData[6]){
                $sql = "INSERT INTO tblproductdata(strProductCode, strProductName, strProductDesc, intStock, intPrice, dtmAdded, dtmDiscontinued) VALUES('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$date','$date')";
                echo "test 1";
                }
                else if($var1 <= $emapData[3] && $var3 <= $emapData[4] && $var2 >= $emapData[4] && preg_match("/^[a-zA-Z0-9]+$/", $value) == $emapData[1] && preg_match("[a-zA-Z]", $value) == $emapData[2] && preg_match("[\s]", $value) == $emapData[6]){
                    $sql = "INSERT INTO tblproductdata(strProductCode, strProductName, strProductDesc, intStock, intPrice, dtmAdded, dtmDiscontinued) VALUES('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','null','null')";
                    echo "test 2";
                    }
                else{   
                $write = "$emapData[0], $emapData[1], $emapData[2], $emapData[3], $emapData[4], $emapData[5], $emapData[6], $emapData[7]

";   
                 file_put_contents($filetxt , $write , FILE_APPEND);
                }
                $res=$conn->query($sql);
            }
            echo "$sql";
            fclose($file);
            echo "CSV File has successfully been Imported";
        }
        else
        {
            echo "Invalid File: Please Upload a Valid CSV File";
        }
    }
header("Location: index.php");
?>

I especially need help with adding the unsuccessful database entries to a PDF.

  • 写回答

1条回答 默认 最新

  • douhan9191 2015-07-02 13:36
    关注

    Storing current date and time

    MySQL supports two different formats for storing dates with times, datetime and timestamp. I will show examples with timestamp here.

    You can also tell MySQL to fill your field with the current date and time whenever a row is inserted (read more here). The SQL for creating the table then looks somewhat like this:

    CREATE TABLE test (
      name CHAR(30),
      time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )
    

    If you insert into the table without setting a value for time, it will set the current time:

    INSERT INTO(name) VALUES("some text")
    

    You can also set the value manually:

    INSERT INTO(name, time) VALUES("some text", CURRENT_TIMESTAMP)
    

    Writing to a PDF

    Have a look at FPDF. However, for just writing an error log a normal text file would be much simpler and perhaps more suitable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常