我有这个上传 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>";