Php 无法将CSV文件提交到上载程序

Php 无法将CSV文件提交到上载程序,php,csv,Php,Csv,我上传了一个CSV文件,用户必须告诉程序CSV文件中某些列的确切名称,以便它可以访问和获取数据存储在数据库中。我有一些错误信息,每当他们没有填写必要的内容时,就会把他们带回到表格中,以纠正他们所犯的错误。问题是它总是让我回头,我真的不知道为什么 上传程序代码--------------------------------------------------------------------- try { # MySQL with PDO_MYSQL $DBH = new PDO

我上传了一个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.";   
    }
}
谢谢你抽出时间

更新---------------------------

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>

CSV上传程序
输入“名字”列的名称
:

我不确定数据文件的外观,但您可能希望在
fgetcsv
ed数组值上使用
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\u search($\u POST[“firstname”],$fileop)
返回0(这是数组中的第一个索引),你的
空($firstname\u index)
将是
TRUE
,你将被踢回去

你应该使用

if($firstname_index===false)
相反(在所有四个索引中)

编辑#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*/

我永远不会知道的数据文件,所以他们告诉我们firstname列、lastname等的名称。然后它检查(索引)以查看该值是否存在。如果它这样做了,它将循环并将所有值添加到db中。我会尝试你所说的,让你知道。我仍然会被踢回表单-它会使用returnBack返回错误function@DavidBiga然后,您应该使用一些调试技术,看看哪里出了问题……您是否同时使用了
trim
和FASLE检查?你有没有考虑过让你的标题匹配不区分大小写?我马上就要看了-你能不能用不区分大小写的匹配来启发我,因为我可能应该这样做。非常感谢你抽出时间!你也可以添加你的HTML代码吗?我指的是形式。这个问题的变体。。。