我對我們公司的競賽形式寫入到csv文件之前檢查重複。每個家庭只有一個入口,所以在提交時,我想檢查提交的地址是否已經在寫入之前輸入到csv文件中。我所看到的,你可以使用SELECT有一個數據庫,但我不知道如何與.csv
這裏做的是代碼:PHP - 寫入CSV文件
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 ."\n";
if(empty($errorMessage)){
$fs = fopen("contest.csv","a");
fwrite($fs,$csvData);
fclose($fs);
header("location: thank you");
exit;
}
}
我想改變我的fopen到r+
在代替a
然後讀取csv文件以檢查地址。然後把另一個if語句傳遞給「重複發現」或其他fwrite
的信息並將它們發送到感謝頁面。
你能不能在這裏使用一個數據庫? – Kit
您必須閱讀csv文件並解析每行以查看該變量是否匹配。你看過[SQLite](http://www.sqlite.org)嗎?它爲文本文件提供了一個SQL機制。然後可以將完整的數據庫導出爲CSV,以便隨後導入到電子表格中 –
平面文件在某些情況下確實有用,但這不是其中之一。 –