创建CSV上传到Mysql

时间:2013-01-29 13:56:26

标签: php mysql csv upload

我在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文件的图片: enter image description here

2 个答案:

答案 0 :(得分: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> 

答案 1 :(得分:-1)

我自己没有用过,但你试过这个吗,

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

你也可以尝试在\ n上使用explode将csv分解成行,然后根据,进一步将其拆分。

一旦你把所有的块都推到了数据库中。