我有这个上传CSV并将数据插入数据库的PHP代码,一旦它在while循环中上传,它只从CSV中选择一行:
//do upload
if (is_uploaded_file($_FILES['dd_submission_form']['tmp_name']))
{
echo "<h3>" . "File ". $_FILES['dd_submission_form']['name'] ." uploaded successfully." . "</h3>";
echo '<h3>Display file contents:</h3>';
//readfile($_FILES['dd_submission_form']['tmp_name']);
}
//Import uploaded file to Database
$handle = fopen($_FILES['dd_submission_form']['tmp_name'], "r");
fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql="SELECT * from customer where directdebit_reference = '".$data[0]."' ";
echo $sql;
$rs=mysql_query($sql,$conn) or die(mysql_error());
$customer=mysql_fetch_array($rs);
if(mysql_num_rows($rs) > 0)
{
$sql2="INSERT into dd_submissions (customer_seq, dd_reference, sortcode, account_number, account_name, amount, bacs_code, invoice_no, title, initial, forename, surname, salutation_1, salutation_2, address_1, address_2, area, town, postscode, phone, mobile, email, notes) values ('".$customer["sequence"]."', '$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[9]', '$data[10]', '$data[11]', '$data[12]', '$data[13]', '$data[14]', '$data[15]', '$data[16]', '$data[17]', '$data[18]', '$data[19]', '$data[20]', '$data[21]', '$data[22]')";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
}
}
fclose($handle);
print "<br><br><h3>Successfully Imported Clients</h3><br>";
答案 0 :(得分:4)
只是单独解决循环之前的第一行,并且不执行任何操作以跳过它。
fgets($handle);
然后你的循环
答案 1 :(得分:2)
最简单的方法是在continue
循环中使用while
关键字,如下所示:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($i == 0) { $i++; continue; }
...
$i++;
但是,为了安全起见,您必须确保第一行不包含必需的数据。
答案 2 :(得分:0)
我建议您只需调用mySQL的LOAD DATA INFILE
查询来替换所有这些代码,该查询旨在将CSV文件直接加载到数据库中而无需您需要的所有PHP循环这样做。它比在PHP循环中运行大量更快。
您可以使用子查询轻松添加您的客户字段,作为主LOAD DATA INFILE
查询的一部分。
哦,为了回答原始问题,它还可以跳过第一行(或行)作为标准语法的一部分。
请参阅mySQL手册页:http://dev.mysql.com/doc/refman/5.1/en/load-data.html
<强> [编辑] 强>
您的查询大致如下:
LOAD DATA INFILE
INTO TABLE dd_submissions
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROW
(
@dd_reference,
sortcode,
account_number,
account_name,
amount,
bacs_code,
invoice_no,
title,
initial,
forename,
surname,
salutation_1,
salutation_2,
address_1,
address_2,
area,
town,
postscode,
phone,
mobile,
email,
notes
)
SET
customer_seq = SELECT sequence from customer where directdebit_reference = @dd_reference,
dd_reference = @dd_reference
;
答案 3 :(得分:0)
我怀疑你遇到的问题可能是因为“1000”,这无论如何都是不必要的。跳过第一行的简单方法是执行以下操作:
fgetcsv($handle) //skips the first line
while($data = fgetcsv($handle)) { //will process second line to end of file
...
}
但是,这里有一个更大的问题。如果您的csv文件如下所示:
'; DROP TABLE customer #
然后$ sql变为
SELECT * from customer where directdebit_reference = ''; DROP TABLE customer #'
现在你没有顾客。查找SQL注入以了解如何确保传递给MySQL的查询是安全的。
答案 4 :(得分:0)
从csv文件中断行读取行的一个可能原因是csv文件中的数据可能包含单引号或双引号,这会破坏sql查询,因此可以使用{{3功能。所以你的新插入查询看起来像:
....
......
$sql2="INSERT into dd_submissions (customer_seq, dd_reference, sortcode, account_number, account_name, amount, bacs_code, invoice_no, title, initial, forename, surname, salutation_1, salutation_2, address_1, address_2, area, town, postscode, phone, mobile, email, notes) values ('".mysql_real_escape_string($customer["sequence"])."', '".mysql_real_escape_string($data[0])."', .......);
希望它有所帮助...
答案 5 :(得分:0)
在while loop
之前声明一个变量:
$row=1;
然后在while loop
中提出了这个条件:
if($row==1){
$row++;
continue;
}