我正在进行PHP分配,并收集有关用户的信息并将其存储在文件中,具体取决于用户是否订阅。 免责声明:我知道在数据库和使用mySQL中做这些事情会更好,但我们还没有学到它,并被告知要使用PHP。我有单选按钮,用于确定是订阅还是取消订阅。这些是
<input type="radio" name="sub" value="subscribe" checked="checked" />
<input type="radio" name="sub" value="unsubscribe" />
我有默认选中的订阅者。我在下面列出的变量$Action
中得到了单选按钮的结果。但是出于某种原因,每当我尝试“订阅”时,我总会得到die
消息,“无法创建文件!”我不知道为什么我会得到它。以下是我的代码。
$Name = htmlspecialchars($_POST['name']);
$EmailAddress = htmlspecialchars($_POST['emailaddress']);
$Action = $_POST['sub'];
if($EmailAddress == "") {
print "<p>Error. No email address. Please try again.</p>";
$move = 0;
} else {
$move = 1;
}
if($move) {
// If the user subscribes, make a file for them.
if($Action == "subscribe") {
// Create a file for each user saved as their email address.
$myfile = fopen($_POST['emailaddress'] . ".txt", "w") or die("Unable to create file!");
$txt = "Name: " . $_POST['name'] . "\n"; // Add the name to the file
fwrite($myfile, $txt);
$txt = "Email Address: " . $_POST['emailaddress'] . "\n"; // Add the email address to the file
fwrite($myfile, $txt);
// Check which preferences are set. Add to file if they are checked.
if(isset($_POST['compositions'])) {
$txt = "Preference: " . $_POST['compositions'] . "\n"; // Add the first preference to the file if checked
fwrite($myfile, $txt);
}
if(isset($_POST['marchingband'])) {
$txt = "Preference: " . $_POST['marchingband'] . "\n"; // Add the second preference to the file if checked
}
if(isset($_POST['projects'])) {
$txt = "Preference: " . $_POST['projects'] . "\n"; // Add the third preference to the file if checked
}
if(isset($_POST['events'])) {
$txt = "Preference: " . $_POST['events'] . "\n"; // Add the fourth preference to the file is checked
}
fclose($myfile);
print "<p>Thank you for subscribing!</p>";
} else {
// If a user unsubscribes, delete their file.
unlink($_POST['emailaddress'] . ".txt");
print "<p>You have successfully unsubscribed.</p>";
}
}
任何人都可以帮助我理解为什么每次填写所有必填字段时都会收到错误消息,比如我应该这样做?感谢。