2012-08-17 53 views
0

我上傳了一個CSV文件,用戶必須告訴程序CSV文件中某些列的確切名稱,以便它可以訪問和獲取數據以存儲在數據庫中。我有一些錯誤消息,只要他們沒有填寫需要的東西,就會將他們帶回來形成解決他們犯的錯誤。問題是它總是把我帶回來,我不知道爲什麼。無法向上傳者提交CSV文件

代碼爲上傳者------------------------------------------- --------------------------

try { 
    # MySQL with PDO_MYSQL 
    $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); 
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

} 
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that."; 
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); 
} 

function returnBack(){ 

    header("Location:csvuploader/csvuploaderform.php?seterror=1"); 
    exit; 

} 

if (isset($_POST['submit'])){ 

    /*******************************GET NAME OF COLUMNS IN CSV FILE *********************************************************/ 
    if (empty($_POST['files'])){ 
     returnBack(); 
    } 
    if (empty($_POST['firstname'])){ 
     returnBack(); 
    } 
    if (empty($_POST['lastname'])){ 
     returnBack(); 
    }if (empty($_POST['email'])){ 
     returnBack(); 
    } 
    if (empty($_POST['phone'])){ 
     returnBack(); 
    } 
    $file = $_FILES['files']['tmp_name']; 

    $handle = fopen($file , "r"); 

    $fileop = fgetcsv($handle,1000,","); 

    $firstname_index = array_search($_POST['firstname'],$fileop); 
    if (empty($firstname_index)){ 
     returnBack(); 
    } 
    $lastname_index = array_search($_POST['lastname'],$fileop); 
    if (empty($lastname_index)){ 
     returnBack(); 
    } 
    $email_index = array_search($_POST['email'],$fileop); 
    if (empty($email_index)){ 
     returnBack(); 
    } 
    $phone_index = array_search($_POST['phone'],$fileop); 
    if (empty($phone_index)){ 
     returnBack(); 
    } 

    /***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEN INTO CSV TABLE IN DB *************************************/ 


    while (($fileop = fgetcsv($handle,1000,",")) !== false) 
    { 
     $firstname = $fileop[$firstname_index]; 
     $lastname = $fileop[$lastname_index]; 
     $email = $fileop[$email_index]; 
     $phone = $fileop[$phone_index]; 

     $insertdata = $DBH->prepare("INSERT INTO csv (firtname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')"); 
     $insertdata->execute(); 
    } 
    if ($insetdata){ 
     echo "Successfully Uploaded. Thank you."; 
    } 
} 

謝謝你的時間!

修訂---------------------------

表單代碼

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> 
<tr> 
<form method="post" action="csvuploader.php" enctype="multipart/form-data"> 
<td> 
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 
<tr> 
<td colspan="3"><strong>CSV Uploader </strong></td> 
</tr> 
<tr> 
<td>Enter Name of First Name Column </td> 
<td>:</td> 
<td><input name="fisrtname" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){ 
    echo "<span class='errorMsg'>Need Exact Name</span>"; 
    } 
     ?> </td> 
</tr> 
<tr> 
<td>Enter Name of Last Name Column </td> 
<td>:</td> 
<td><input name="lastname" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){ 
    echo "<span class='errorMsg'>Need Exact Name</span>"; 
    } 
     ?></td> 
</tr> 
<tr> 
<td>Enter Name of Email Column </td> 
<td>:</td> 
<td><input name="email" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){ 
    echo "<span class='errorMsg'>Need Exact Name</span>"; 
    } 
     ?></td> 
</tr> 
<tr> 
<td>Enter Name of Phone Number Column </td> 
<td>:</td> 
<td><input name="phone" type="text" ><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){ 
    echo "<span class='errorMsg'>Need Exact Name</span>"; 
    } 
     ?></td> 
</tr> 
<tr> 
<td width="294"><input type="file" name="files" /><?php 
    $setError=$_GET['seterror']; 
    if($setError == 1){ 
    echo "<span class='errorMsg'>Select a File</span>"; 
    } 
     ?></td> 
</tr> 
<br /> 
<tr> 
<td><input type="submit" name="submit" value="Submit" /></td> 
</tr> 
</table> 
</td> 
</form> 
</tr> 
</table> 
+0

您可以添加HTML代碼呢?我的意思是表格。 – 2012-08-17 03:37:40

+0

是的,我現在會這麼做 – 2012-08-17 03:39:02

+0

好的,謝謝:) – 2012-08-17 03:43:54

回答

1

我不知道如何您的數據文件的樣子,但你可能想在fgetcsv編輯數組值使用trim,因爲最後的值很可能包括行尾:

$fileop=fgetcsv($handle); 
$fileop=array_map("trim",$fileop); 
/*recognize index*/ 
while (($fileop=fgetcsv($handle))!==false) 
{ 
    $fileop=array_map("trim",$fileop); 
    $firstname=$fileop[$firstname_index]; 
    /*insert into DB*/ 
} 

編輯

哎呀,好像我錯過了什麼?你用

if(empty($firstname_index)) 

在你的代碼,但如果,例如,array_search($_POST["firstname"],$fileop)返回0(這是第一個索引數組),您empty($firstname_index)將是TRUE,你會被踢回去。

您應該使用

if($firstname_index===false) 

,而不是(在所有四個指標)。 PHP Document

編輯#2

此外,這是更好地使你的標題匹配的不區分大小寫:

$fileop=fgetcsv($handle); 
$fileop=array_map("strtoupper",array_map("trim",$fileop)); 
$firstname_index=array_search(strtoupper($_POST["firstname"]),$fileop); 
if($firstname_index===false) returnBack(); 
/*and all four indexes*/ 
+0

數據文件我永遠不會知道,所以他們告訴我們名字列,姓氏等名稱。然後它檢查(索引)以查看該值是否存在。如果它確實會循環並將所有值添加到db。我會嘗試你所說的並讓你知道。 – 2012-08-17 03:35:33

+0

我仍然被踢回表單 - 它使用returnBack函數繼續發送錯誤 – 2012-08-17 03:38:28

+0

@DavidBiga現在怎麼樣? – Passerby 2012-08-17 03:48:20