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.

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/