1

我在 MYSQL 中有一个表,我希望我的客户可以选择上传 CSV 文件。默认情况下,我希望它添加不存在的信息,并在有信息时自动更新它。

这些是我的专栏:

id  customer_name   customer_name_letterhead    customer_notes  systype status  signaltype  verification    address postcode    telephone   mobile  mobiletwo   email   mainarea    installation    Contract    expiration  SPA nservice    maintenance monitoring  MS  certdate

我已经知道我需要在行的标题中包含所有这些的 excel 文档。例如 A1-V1 有这些标题。

编辑:我做了这个:

<?PHP    if(isset($_POST['SUBMIT']))
    {
         $fname = $_FILES['sel_file']['name'];

         $chk_ext = explode(".",$fname);

         if(strtolower($chk_ext[1]) == "csv")
         {

             $filename = $_FILES['sel_file']['tmp_name'];
             $handle = fopen($filename, "r");

             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
             {
                $sql = "INSERT into Customers(id,customer_name,customer_name_letterhead,customer_notes,systype,status,signaltype,verification,address,postcode,telephone,mobile,mobiletwo,email,mainarea    installation,Contract,expiration,SPA,nservice,maintenance,monitoring,MS,certdate) values('$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]','$data[23]','$data[24]')";
                mysql_query($sql) or die(mysql_error());
             }

             fclose($handle);
             echo "Successfully Imported";
         }
         else
         {
             echo "Invalid File";
         }   
    }?>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>

    Import File : <input type="file" name="sel_file" id="sel_file">
    <input type='submit' name='submit' value='submit'>

</form>

单击提交后,没有任何反应。

这是我的 csv 文件的图片: 在此处输入图像描述

4

2 回答 2

0

我最终在 YouTube 上搜索了一个确实有帮助的视频。它最终给了我这个:

<?PHP   

if(isset($_POST['submit'])) 
{ 
    $file = $_FILES['file']['tmp_name']; 

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

    while(($fileop = fgetcsv($handle,1000, ",")) !== false) 
    { 

    $customer_name             = $fileop[0]; 
    $customer_name_letterhead     = $fileop[1]; 
        $customer_notes         = $fileop[2]; 
        $systype             = $fileop[3]; 
        $status             = $fileop[4]; 
        $signaltype             = $fileop[5]; 
        $verification             = $fileop[6]; 
        $address             = $fileop[7]; 
        $postcode            = $fileop[8]; 
        $telephone             = $fileop[9]; 
        $mobile             = $fileop[10]; 
        $mobiletwo             = $fileop[11]; 
        $email                 = $fileop[12]; 
        $mainarea             = $fileop[13]; 
        $installation            = $fileop[14]; 
        $Contract             = $fileop[15]; 
        $expiration             = $fileop[16]; 
        $SPA                 = $fileop[17]; 
        $nservice            = $fileop[18]; 
        $maintenance             = $fileop[19]; 
        $monitoring             = $fileop[20]; 
        $MS                 = $fileop[21]; 
        $certdate             = $fileop[22]; 


        $sql = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO Customers ( 
            customer_name, 
                    customer_name_letterhead, 
                    customer_notes, 
                    systype, 
                    status, 
                    signaltype, 
                    verification, 
                    address, 
                    postcode, 
                    telephone, 
                    mobile, 
                    mobiletwo, 
                    email, 
                    mainarea, 
                    installation, 
                    Contract, 
                    expiration, 
                    SPA, 
                    nservice, 
                    maintenance, 
                    monitoring, 
                    MS, 
                    certdate 
                    ) VALUES ( 
                   '$customer_name', 
           '$customer_name_letterhead', 
           '$customer_notes', 
           '$systype', 
           '$status', 
           '$signaltype', 
           '$verification', 
           '$address', 
           '$postcode', 
           '$telephone', 
           '$mobile', 
           '$mobiletwo', 
           '$email', 
           '$mainarea', 
           '$installation', 
           '$Contract', 
           '$expiration', 
           '$SPA', 
           '$nservice', 
           '$maintenance', 
           '$monitoring', 
           '$MS', 
           '$certdate' 
                   )"); 

                      if($sql) 
                      { 

                        echo 'Uploaded '. $sql . ' entries'; 
                      }      
    } 
} 

?> 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">  
    <input type="file" name="file"> 
    <br> 
    <input type="submit" name="submit" value="Upload CSV File">  
</form> 
于 2013-01-29T17:48:18.340 回答
-1

自己没用过 但是你试过吗

http://php.net/manual/en/function.str-getcsv.php

您也可以尝试使用explode on \n将csv分成几行,然后根据,.

一旦你有所有的块将它推入数据库。

于 2013-01-29T14:00:13.853 回答