0

我已经上传了一个 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>
4

1 回答 1

1

我不确定您的数据文件的外观,但您可能希望trimfgetcsved 数组值上使用,因为最后一个值很可能包含行尾:

$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 文档.

编辑#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*/
于 2012-08-17T03:31:41.510 回答