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 fwrite
s the info and sends them to the thank you page.