[英]Error when uploading CSV file to database using PHP
I'm trying to upload a CSV file into an existing table in my database using PHP. 我正在尝试使用PHP将CSV文件上传到数据库中的现有表中。
Here is my full code: 这是我的完整代码:
<?php
include("detail.php");
$connect = mysql_connect("$host", $user, $password) or die("Couldn't connect to SQL Server on $myServer");
mysql_select_db("$database") or die("Couldn't open database $myDB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
define('CSV_PATH','E:/4th Year/FYP/'); // specify CSV file path
$csv_file = CSV_PATH . "Cash2011.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['cashAmount'] = $csv_array[0];
$insert_csv['cashReceivedDate'] = $csv_array[1];
$insert_csv['customerID'] = $csv_array[2];
$insert_csv['invoiceNo'] = $csv_array[3];
$insert_csv['monthNum'] = $csv_array[4];
$insert_csv['yearNum'] = $csv_array[5];
$query = "INSERT INTO cash2011(cashAmount,cashReceivedDate,invoiceNo,monthNum,yearNum) VALUES ('','".$insert_csv['cashAmount']."','".$insert_csv['cashReceivedDate']."','".$insert_csv['customerID']."','".$insert_csv['invoiceNo']."','".$insert_csv['monthNum']."','".$insert_csv['yearNum']."')";
$n=mysql_query($query, $connect);
$i++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
?>
These are the errors that I keep receiving: Undefined offset: 1 in C:\\Apache2.2\\htdocs\\clearTables.php on line 47 (which is $insert_csv['cashAmount'] = $csv_array[0];). 这些是我一直收到的错误:未定义的偏移量:第47行的C:\\ Apache2.2 \\ htdocs \\ clearTables.php中的1($ insert_csv ['cashAmount'] = $ csv_array [0];)。 Undefined offset: 2 in C:\\Apache2.2\\htdocs\\clearTables.php on line 48.
未定义的偏移量:第48行的C:\\ Apache2.2 \\ htdocs \\ clearTables.php中的2。
Does anyone know what the problem is? 有人知道问题出在哪里吗? Thanks
谢谢
i tested your code, and work very nice... and, when i echo the query, return: 我测试了您的代码,并且工作得非常好……并且,当我回显查询时,返回:
INSERT INTO cash2011(cashAmount,cashReceivedDate,invoiceNo,monthNum,yearNum)
VALUES ('','ad4213','ad4214','ad4215','ad4216','adware','ad7777 ')
//'','ad4213','ad4214','ad4215','ad4216','adware','ad7777 ' -> data in my example csv file
a few things.. i believe that your file doesn't exist, that is why you have those errors, try to check with this line before the while (!feof($file))
一些事情..我相信您的文件不存在,这就是为什么您有这些错误,请尝试在
while (!feof($file))
之前与这一行进行检查while (!feof($file))
if(is_file($csv_file)){ //your code}
also, in your query, you have 5 values, and only 4 in the rows definition. 此外,在查询中,您有5个值,而行定义中只有4个值。 and, why you have a
,
there? 并且,为什么你有一个
,
在那里?
if your problem continue, check my answer here , if you look, is a similar code of yours. 如果您的问题仍然存在,请在此处检查我的答案,如果您看的话,与您的代码相似。
you have syntax error in following code. 您在以下代码中有语法错误。 So replace
所以更换
$query = "INSERT INTO cash2011(cashAmount,cashReceivedDate,invoiceNo,monthNum,yearNum) VALUES ('','".$insert_csv['cashAmount']."','".$insert_csv['cashReceivedDate']."','".$insert_csv['customerID']."','".$insert_csv['invoiceNo']."','".$insert_csv['monthNum']."','".$insert_csv['yearNum']."')";
By 通过
$query = "INSERT INTO cash2011(cashAmount,cashReceivedDate,invoiceNo,monthNum,yearNum) VALUES ('".$insert_csv['cashAmount']."','".$insert_csv['cashReceivedDate']."','".$insert_csv['customerID']."','".$insert_csv['invoiceNo']."','".$insert_csv['monthNum']."','".$insert_csv['yearNum']."')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.