dongshun5963 2013-01-29 13:56
浏览 79
已采纳

创建CSV上传到Mysql

I have a table in MYSQL where I wish to have the option that my customer can upload a CSV file. By default I want this to add information which isn't there and automatically update it if there is information there.

These are the columns I have:

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

I already know that I need to have an excel document with all of these in the headers of the rows. E.g A1-V1 has those headers.

edit: I have made this:

<?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>

Upon clicking submit, nothing happens.

Here is a picture of my csv file: enter image description here

  • 写回答

2条回答 默认 最新

  • dongshi949737 2013-01-29 17:48
    关注

    I ended up searching for a video on YouTube which really did help. It ended up giving me this:

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了