dongyue8640 2014-09-01 14:25
浏览 54
已采纳

PHP我的管理员没有忽略用于导入CSV文件的重复行选项[重复]

This question already has an answer here:

I have a CSV file that I successfully imported into a database table.

Over time I will update this CSV file with new data so I was wondering if I import the same file which will contain rows that has already been inserted into the table, is there a way to avoid duplicates being added again?.

When researching I came across an option called "Ignore duplicate rows" however this is not present for me within the import options.

I am using the PHP my admin that is packaged inside XAMPP.

which is •Version information: 4.2.7.1, latest stable version: 4.2.8

</div>
  • 写回答

1条回答 默认 最新

  • dongou3158 2014-09-01 16:28
    关注

    add unique key to your database column which you think should be unique

    here is the code i have done for uploading csv files which will stop inserting found values maybe it will help you.

    using On Duplicate Key Update

    <?php
    $conn=mysql_connect("localhost","root","");
    $db=mysql_select_db("dbname");
    
    
     $csv_file="/path/to/file";
     $savefile="filename.csv";
     $handle = fopen($csv_file, "r");
    
    $i=0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { //2nd param is for memory limit use it if u know what u are doing
        if($i>0){
    
            $import="INSERT into table_name(col0,col1,col2,col3)values('".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."')ON DUPLICATE KEY UPDATE col2= '".addslashes($data[1])."'";
            mysql_query($import) or die(mysql_error());
        }
        $i=1;
    }   
    ?>   
    

    cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图