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