dongnong3019 2011-11-30 23:33
浏览 39

PHP - 在写入CSV文件之前检查重复

I have a contest form for our company that writes to a csv file. It is only one entry per household, so at time submission I want to check to see if the submitted address has already been entered into the csv file before it is written. I have seen where you can use SELECT with a database, but I don't know how to do it with a .csv Here is the code:

if($_POST['formSubmit'] == "Enter Now") {

         $errorMessage = "";
         $aClass = "";

         if(empty($_POST['name'])){$errorMessage .= "<li>You forgot to enter your name!</li>";}
         if(empty($_POST['email'])){$errorMessage .= "<li>You forgot to enter your email!</li>";}
         if(empty($_POST['address1'])){$errorMessage .= "<li>You forgot to enter your address!</li>";}
         if(empty($_POST['city'])){$errorMessage .= "<li>You forgot to enter your city!</li>";}
         if(empty($_POST['postcode'])){$errorMessage .= "<li>You forgot to enter your zip code!</li>";}

         $name = $_POST['name'];
         $email = $_POST['email'];
         $address1 = $_POST['address1'];
         $city = $_POST['city'];
         $state = $_POST['state'];
         $postcode = $_POST['postcode'];
         $csvData = $name . "," . $email . "," . $address1 . "," . $city . "," . $state . "," . $postcode ."
";

         if(empty($errorMessage)){
              $fs = fopen("contest.csv","a");
              fwrite($fs,$csvData);
              fclose($fs);
            header("location: thank you");
              exit;
         }
    }

I am thinking I change my fopen to a r+ in stead of a and then read the csv file to check for the address. Then put another if statement where it relays 'duplicate found' or else fwrites the info and sends them to the thank you page.

  • 写回答

1条回答 默认 最新

  • dphw5101 2011-12-01 00:41
    关注

    As mentioned in some of the comments, flatfiles are not the best solution for this type of problem, but as you mentioned sometimes it's just easier (and cheaper) to use what's already there and move onto other features.

    If you were using a database, you could add a unique constraint on columns where you don't want duplicate data to appear, and this de-dupping capability is handled for you at the database layer. Although you would have to handle the error returned when it attempted to do an insert, its a more elegant solution.

    However, if you want to move forward with your CSV solution you will have to parse the existing CSV and extrapolate out the existing addresses, then you can check if the form submitted address already exists.

    // Pseduo code
    User submits form
    
        Open up existing CSV file
    
        Step through each entry in csv file
            Store Address in $addresses[] array
    
        Check if user submitted address exists in $addresses[] array
    

    As you can see this solution is non-performant, in that you will have to open, parse, and check every time the form is submitted, however it should accomplish your goal.

    Let me know if the psuedo code isn't enough, I can code it out.

    --Edit--

    Below are a few links to get you started on how to integrate a database solution to solve your problem. These are just to get you started, as there is a wealth of information on the subject:

    Database design: http://en.wikipedia.org/wiki/Database_design

    Vanilla interaction with database (using PHP): http://php.net/manual/en/book.pdo.php

    ORM interaction with database (using PHP): http://www.doctrine-project.org/

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么