1

我目前正在开发一个 CSV Uploader 项目,并且它可以正常工作,但我的问题是 - 如果客户 CSV 文件以不同方式组合在一起,我将如何选择它(名字、姓氏、电子邮件、电话等)。或者上传文件的客户是否必须按照程序的制作顺序来拥有它?

这是php代码----------------------------------------------------------

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);   
}

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

    $file = $_FILES['files']['tmp_name'];   

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

    while (($fileop = fgetcsv($handle,1000,",")) !== false)
    {
        $firstname = $fileop[0];
        $lastname = $fileop[1];
        $email = $fileop[2];
        $phone = $fileop[3];

        $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:http://pcgprotects.com/PCG/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.";   
        }
    }

在我的 csv 试用文件中,我使用了名为 - 名字、姓氏、电话、电子邮件的列。然后在我的表单中输入了这些值,但我被踢了回来-这是我的表单代码 -----

    <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

0

您可以要求客户在 CSV 的开头添加一行来显示结构,例如:

firstname,lastname,email,phone
joe,smith,j@gmail.com,41595648855
joe2,smith2,j2@gmail.com,41595648852

...

通过阅读第一行 - 您将能够知道您应该使用哪个标记与哪个索引。

$file = $_FILES['files']['tmp_name'];   
$handle = fopen($file , "r");
$fileop = fgetcsv($handle,1000,",");
$firstname_index = array_search("firstname",$fileop);
$lastname_index = array_search("lastname",$fileop);
...
//and then
while (($fileop = fgetcsv($handle,1000,",")) !== false)
{
  $firstname = $fileop[$firstname_index];
  $lastname = $fileop[$lastname_index];
  ...
于 2012-08-16T22:06:42.230 回答