如何使用php导入数据库中当前日期存储的csv文件

时间:2016-05-12 10:49:33

标签: php mysql

我在没有日期格式的mysql数据库中导入csv文件。但是我必须在单独的列中插入当前数据和时间。我可以实现代码来存储日期和时间。

php代码:              

       include_once("includes/dbConnection.php");
       session_start();
         if(!isset($_SESSION['u_id']))
              {
             header("Location:membership_import_csvpage.php");
                }
               //$deleterecords = "TRUNCATE TABLE contacts";
            //empty the   table of its current records
          //mysql_query($deleterecords);

       if ($_FILES[csv][size] > 0) { 

          $file = $_FILES[csv][tmp_name]; 
          $handle = fopen($file,"r"); 
         $date = date('Y-m-d');
           do { 
             if ($data[0]) { 
          mysql_query("INSERT INTO membership (mbid, name,identification_number,date,user_id) VALUES 
            ( 
                '".addslashes($data[0])."', 
                '".addslashes($data[1])."', 
                '".addslashes($data[2])."',
                  '".addslashes($data  ['CURDATE()'])."',
                 '".addslashes($_SESSION['u_id'])."'                    
            ) "); 
             } 
        }                while ($data = fgetcsv($handle,1000,",","'")); 
             // 

              //redirect 
           echo ("<SCRIPT LANGUAGE='JavaScript'>
           window.alert('import csv Updated successfully')
   window.location.href='membership_import_csvpage.php';
          </SCRIPT>");

       die; 

     } 
     ?> 

1 个答案:

答案 0 :(得分:0)

尝试将其用于csv文件

<?php 
    $connect = mysqli_connect("localhost", "root", "", "testing");
    if(isset($_POST["submit"]))
{
    if($_FILES['file']['name'])
    {
        $filename = explode(".", $_FILES['file']['name']);
        if($filename[1] == 'csv')
        {
            $handle = fopen($_FILES['file']['tmp_name'], "r");
            while($data = fgetcsv($handle))
            {
                $item1 = mysqli_real_escape_string($connect, $data[0]);  
                $item2 = mysqli_real_escape_string($connect, $data[1]);
                $query = "INSERT into excel(excel_name, excel_phone) values('$item1','$item2')";
                mysqli_query($connect, $query);
            }
            fclose($handle);
            echo "<script>alert('Import done');</script>";
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <style>
        table{
            border-collapse: collapse;
            width: 100%;
            color: #588c7e;
            font-family: monospace;
            font-size: 25px;
            text-align: left;
        }
        th{
            background-color: #588c7e;
            color: white;
        }
        tr:nth-child(even) {background-color: #f2f2f2}
    </style>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <div align="center">
            <label>Select CSV File:</label>
            <input type="file" name="file" />
            <br />
            <input type="submit" name="submit" value="Import" class="btn btn-info" />
        </div>
    </form><br><br>
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Phone_No</th>
            <?php
            $conn = mysqli_connect("localhost", "root", "", "testing");
            // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 
                $sql = "SELECT excel_id, excel_name, excel_phone FROM excel";
                $result = $conn->query($sql);
                if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) {
                        echo "<tr><td>" . $row["excel_id"]. "</td><td>" . $row["excel_name"] . "</td><td>"
                        . $row["excel_phone"]. "</td></tr>";
                    }
                    echo "</table>";
                } else { echo "<script>alert('0 Results');</script>"; }
                $conn->close();
            ?>
        </tr>
    </table>
</body>
</html>