上傳

2013-08-02 37 views
1

當PHP跳過第一行中CSV我有上載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>"; 

回答

2

最簡單的方法是使while循環內使用continue關鍵字像如下:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    if ($i == 0) { $i++; continue; } 
    ... 
    $i++; 

不過,爲了安全起見,你必須確保第一行不包含所需的數據。

+0

這將跳過每行作爲侑$ i ++在是繼續:) – Trent

+0

@Trent後,你'對,謝謝。忽略事情很簡單,不是嗎? :) – vee

+0

它發生在更簡單的答案上更頻繁;我們可能從現有代碼 – Trent

4

只需分別在循環之前的第一行,不做任何事情就可以跳過它。

fgets($handle);那麼你的循環

+0

複製和粘貼一千次,看到我編輯的代碼 – charlie

+0

和問題... – charlie

+0

@charlie對不起,不確定。對於編碼來說很新穎,現在可以更好地提出一個新問題,或者嘗試下面發佈的方法vinodadhikary。我只知道如何跳過自己解決這個問題的第一行:p – robz228

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 
; 
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的查詢是安全的。

0

中斷從csv文件中讀取行的一個可能原因是,您的csv文件中的數據可能包含打破sql查詢的單引號或雙引號,因此可以使用mysql_real_escape_string函數來解決此問題。因此,您的新插入查詢將如下所示:

.... 
...... 

$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])."', .......); 

希望它有幫助...

0

聲明一個變量之前while loop

$row=1; 

然後在while loop把這個條件:

if($row==1){ 
    $row++; 
    continue; 
}