0
我正在嘗試搜索文本文件以查找輸入到表單中的變量,並檢查是否有重複項。該程序將搜索多個變量(名字,姓氏和電子郵件)。我能夠搜索一個變量並讓程序返回已經有一個條目,但是一旦我添加了第二個變量,我就會遇到各種各樣的麻煩。目前的代碼只是搜索名字和姓氏。我可以在成功找到兩個當前的變量後添加第三個變量。任何幫助將不勝感激。根據文件檢查多個變量
我使用的代碼:
<!DOCTYPE html>
<html>
<body>
<?php
if(isset($_POST['submit'])) {
$fname = stripslashes($_POST['firstname']);
$lname = stripslashes($_POST['lastname'] . PHP_EOL);
$existingnames = array();
$namefile = fopen("test_dupes.txt", "a+");
if(file_exists("test_dupes.txt")){
$existingnames = file("test_dupes.txt");
if(in_array($fname, $existingnames)){
if(in_array($lname, $existingnames)){
echo "<p>The name is already in the file!<br>";
echo "Please enter another name.</p>";
}
} else {
fwrite($namefile, "$fname" . " ");
fwrite($namefile, "$lname");
fclose($namefile);
echo "Your name has been successfully added to the list.";
}
} else {
if($namefile === false) {
echo "There was an error saving your name to the file.";
} else{
fwrite($namefile, "$fname" . " ");
fwrite($namefile, "$lname");
fclose($namefile);
echo "Your name has been successfully to the list.";
}
}
}
?>
<form action="test_dupes.html">
<input type="submit" value="Return to Name Entry" />
</form>
</body>
</html>
這對我有效。非常感謝你。我假定當$ fname被添加到文件時,它會保留自己作爲$ fname而不是與$ lname相結合。 – 2014-10-06 15:10:37