Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Check for duplicate before writing to CSV file
    text
    copied!<p>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 <code>.csv</code> Here is the code:</p> <pre><code>if($_POST['formSubmit'] == "Enter Now") { $errorMessage = ""; $aClass = ""; if(empty($_POST['name'])){$errorMessage .= "&lt;li&gt;You forgot to enter your name!&lt;/li&gt;";} if(empty($_POST['email'])){$errorMessage .= "&lt;li&gt;You forgot to enter your email!&lt;/li&gt;";} if(empty($_POST['address1'])){$errorMessage .= "&lt;li&gt;You forgot to enter your address!&lt;/li&gt;";} if(empty($_POST['city'])){$errorMessage .= "&lt;li&gt;You forgot to enter your city!&lt;/li&gt;";} if(empty($_POST['postcode'])){$errorMessage .= "&lt;li&gt;You forgot to enter your zip code!&lt;/li&gt;";} $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 ."\n"; if(empty($errorMessage)){ $fs = fopen("contest.csv","a"); fwrite($fs,$csvData); fclose($fs); header("location: thank you"); exit; } } </code></pre> <p>I am thinking I change my fopen to a <code>r+</code> in stead of <code>a</code> and then read the csv file to check for the address. Then put another if statement where it relays 'duplicate found' or else <code>fwrite</code>s the info and sends them to the thank you page.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload