gpt4 book ai didi

php - 使用 PHP 将我选择的所有 CSV 文件导入到 Mysql

转载 作者:行者123 更新时间:2023-11-29 22:13:50 26 4
gpt4 key购买 nike

我有一些 PHP 代码将 CSV 数据导入到 Mysql。它工作正常,但此代码仅适用于名为 trendspry.csv 的特定文件。我想添加代码,以便我的程序可以从任何目录选择并导入文件。

我用过:

$file = $_FILES['file']['tmp_name'];

但是当我打印这个时,它总是显示:

C:\\xampp\\tmp\\phpABDC

我做错了什么?

这是我的完整代码:

<?php 
if(isset($_POST['submit']))
{
$connect = mysql_connect('127.0.0.1','root','');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
$cid = mysql_select_db('ts_order_track', $connect);
$q = "LOAD DATA INFILE 'trendspry.csv' REPLACE INTO TABLE tsd_orders
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
( order_id, customer_id, firstname, lastname, email, telephone, mobile, shipping_firstname, shipping_lastname, shipping_mobile, shipping_address_1, shipping_address_2, shipping_city, shipping_zone, shipping_postcode, shipping_country, shipping_method, payment_method, payment_code, weight, date_added, date_modified, import_order_id, sub_total, shipping_charges, tax_charges, total, order_status, courier, awb_code, coupon_amount, coupon_code, product_name, product_sku, product_model, product_quantity, product_price )";
$s = mysql_query($q, $connect);
echo "$q";
echo "File data successfully imported to database!!";
mysql_close($connect);
}
?>

我想更改我的程序以适用于我选择的所有文件。

最佳答案

使用文件名参数:

$filename="abc.csv";    

并将其连接到 $q:

$cid =mysql_select_db('ts_order_track',$connect);
$q="LOAD DATA INFILE '".$filename."' REPLACE INTO TABLE tsd_orders
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
( order_id, customer_id, firstname, lastname, email, telephone, mobile, shipping_firstname, shipping_lastname, shipping_mobile, shipping_address_1, shipping_address_2, shipping_city, shipping_zone, shipping_postcode, shipping_country, shipping_method, payment_method, payment_code, weight, date_added, date_modified, import_order_id, sub_total, shipping_charges, tax_charges, total, order_status, courier, awb_code, coupon_amount, coupon_code, product_name, product_sku, product_model, product_quantity, product_price )";
$s=mysql_query($q, $connect );
echo "$q";

帖子中的文件名是正确的:文件名始终是临时的,因此您可以使用该文件名或检索真实的文件名。

关于php - 使用 PHP 将我选择的所有 CSV 文件导入到 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com