简体   繁体   English

将CSV导入MySQL(WAMP),数据库中不会显示任何数据

[英]Importing CSV into MySQL (WAMP), no data appears in database

I am trying to create a web page that allows end-users to upload a CSV file to a database. 我正在尝试创建一个允许最终用户将CSV文件上载到数据库的网页。 I thought I had this mapped out, and the page behaves like the data goes through. 我以为我把它映射出来了,页面的行为就像数据一样。 When I check my table, however, it's empty. 但是当我检查我的桌子时,它是空的。

I have a guid trigger for table[0] that does not exist in the CSV, so I am trying to fill data in starting at table[1]. 我有一个表[0]的guid触发器,它在CSV中不存在,所以我试图从表[1]开始填充数据。 I know my connection is solid because I am able to query data from the database on a different web page. 我知道我的连接是可靠的,因为我能够在不同的网页上查询数据库中的数据。

Here is my code, after establishing the connection to the database: 在建立与数据库的连接之后,这是我的代码:

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

    //get the csv file
    $file = $_FILES[csv][tmp_name];
    $handle = fopen($file,"r");

    //loop through the csv file and insert into database
    do {
    if ($data[1]) {
        mysqli_query("INSERT INTO bnsf1 (offline, unit, commodity, shipper, origin, originState, current, currentState, estimatedArrival, carType, destination, destinationState, CP_dateTime, waybillNumber, equipmentLength) VALUES
            (
                '".addslashes($data[1])."',
                '".addslashes($data[2])."',
                '".addslashes($data[3])."',
                '".addslashes($data[4])."',
                '".addslashes($data[5])."',
                '".addslashes($data[6])."',
                '".addslashes($data[7])."',
                '".addslashes($data[8])."',
                '".addslashes($data[9])."',
                '".addslashes($data[10])."',
                '".addslashes($data[11])."',
                '".addslashes($data[12])."',
                '".addslashes($data[13])."',
                '".addslashes($data[14])."',
                '".addslashes($data[15])."'
            )
        ");
            }
        } while ($data = fgetcsv($handle,1000,",","'"));
    //

    //redirect
    header('Location: form1.php?success=1'); die;

    }

?>

<!DOCTYPE html>
<html>
<head>
<title>Import a CSV File</title>
</head>

<body>

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>

</body>
</html>

Thank you for looking! 谢谢你的期待!

You have to add connetion variable in you mysqli_query like this. 您必须在mysqli_query中添加连接变量,如下所示。

    mysqli_query(your connection variable,"INSERT INTO bnsf1 (offline, unit, commodity, shipper, origin, originState, current, currentState, estimatedArrival, carType, destination, destinationState, CP_dateTime, waybillNumber, equipmentLength) VALUES
        (
            '".addslashes($data[1])."',
            '".addslashes($data[2])."',
            '".addslashes($data[3])."',
            '".addslashes($data[4])."',
            '".addslashes($data[5])."',
            '".addslashes($data[6])."',
            '".addslashes($data[7])."',
            '".addslashes($data[8])."',
            '".addslashes($data[9])."',
            '".addslashes($data[10])."',
            '".addslashes($data[11])."',
            '".addslashes($data[12])."',
            '".addslashes($data[13])."',
            '".addslashes($data[14])."',
            '".addslashes($data[15])."'
        ) ");

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

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