将CSV数据导入MySQL数据库中的字段

用户3519721

我正在尝试通过网络表单上传CSV文件,该文件包含以下链接中所述的字段[请参阅CSV示例]。用户可以通过单击上载按钮来浏览其计算机,选择其CSV文件,然后单击上载,然后将CSV数据导入到其相应字段中的数据库中。用户上载CSV文件时,该行为空,不包含CSV文件包含的任何数据。有什么办法可以解决这个问题,以便CSV文件中的数据导入到数据库中并放置在其关联字段中?

CSV范例: http
//i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-10at145431_zps80a42938.png

uploads.php

    <!DOCTYPE html>
<head>
    <title>MySQL file upload example</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <form action="upload_file.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file"><br>
        <input type="submit" value="Upload file">
    </form>
    <p>
        <a href="list_files.php">See all files</a>
    </p>
</body>
</html>

upload_file.php

    <?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', 'vario007', 'spineless');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }

        // Gather all required data
        $filedata= file_get_contents($_FILES  ['uploaded_file']['tmp_name']); //this imports the entire file.

        // Create the SQL query
        $query = "
            INSERT INTO `Retail` (
                `date`, `order_ref`, `postcode`, `country`, `quantity`, `packing_price`, `dispatch_type`, `created`
            )
            VALUES (
                '{$date}', '{$order_ref}', '{$postcode}', '{$country}', '{$quantity}', '{$packing_price}', '{$dispatch_type}', NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);


        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="welcome.php">here</a> to go back</p>';
?>
穆尔·杰思洛嘉(Mehul Jethloja)

试试这个

<?php
if(isset($_FILES['uploaded_file'])) {
    if($_FILES['uploaded_file']['error'] == 0) {
        $dbLink = new mysqli('localhost', 'root', 'vario007', 'spineless');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }

        $file = $_FILES  ['uploaded_file']['tmp_name'];
        $handle = fopen($file, "r");
        $row = 1;
        while (($data = fgetcsv($handle, 0, ",","'")) !== FALSE)
        {
            if($row == 1)
            {
                // skip the first row   
            }
            else
            {
                //csv format data like this 
                //$data[0] = date
                //$data[1] = order_ref
                //$data[2] = postcode
                //$data[3] = country
                //$data[4] = quantity
                //$data[5] = packing_price
                //$data[6] = dispatch_type

                $query = "
                INSERT INTO `Retail` (
                `date`, `order_ref`, `postcode`, `country`, `quantity`, `packing_price`, `dispatch_type`, `created`
                )
                VALUES (
                '".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', NOW()
                )";
                $result = $dbLink->query($query);


                 // Check if it was successfull
                if($result) {
                    echo 'Success! Your file was successfully added!';
                }
                else {
                    echo 'Error! Failed to insert the file'
                    . "<pre>{$dbLink->error}</pre>";
                 }
            }
            $row++;
        }



    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="welcome.php">here</a> to go back</p>';
?>

fgetcsv()要保存在csv文件中的分隔符中的第三个参数中。例如:逗号,分号等。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章