我正在创建一个网站,让用户输入信息,然后使用PHP读/写将其保存到计算机上的文档中。一切都很好,除了我有两个错误。
错误是“未定义的偏移:第9行的C:\ wamp \ www \ Phoenix \ ReadWrite.php中的6”和“fopen():C:\ wamp \ www \ Phoenix \ ReadWrite中的文件名不能为空。 php在110行“。 这是代码:
<?php
$nameErr = $ssnErr = $dateErr = $timeErr = $fileErr ="";
$name = $ssn = $date = $time = $comment = $file = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["1"])) {
$nameErr = "Name is required";
$_POST['6'] = "";
} else {
$name = test_input($_POST["1"]);
}
if (empty($_POST["2"])) {
$ssnErr = "SSN is required";
$_POST['6'] = "";
} else {
$ssn = test_input($_POST["2"]);
}
if (empty($_POST["3"])) {
$dateErr = "Date is required";
$_POST['6'] = "";
} else {
$date = test_input($_POST["3"]);
}
if (empty($_POST["4"])) {
$timeErr = "Time is required";
$_POST['6'] = "";
} else {
$time = test_input($_POST["4"]);
}
if (empty($_POST["5"])) {
$comment = "";
} else {
$comment = test_input($_POST["5"]);
}
if (empty($_POST["6"])) {
$fileErr = "Filename is required";
} else {
$file = test_input($_POST["6"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="500" border="0" cellspacing="5" cellpadding="0">
<tr>
<td scope="col">Name:</td>
<td colspan="2" scope="col"><input type="text" name="1"></td>
<td><span class="error"><?php echo $nameErr;?></span></td>
</tr>
<tr>
<td>SSN:</td>
<td colspan="2"><input type="text" name="2"></td>
<td><span class="error"><?php echo $ssnErr;?></span></td>
</tr>
<tr>
<td>Date:</td>
<td colspan="2"><input type="text" name="3"></td>
<td><span class="error"><?php echo $dateErr;?></span></td>
</tr>
<tr>
<td>Time of Arrival:</td>
<td colspan="2"><input type="text" name="4"></td>
<td><span class="error"><?php echo $timeErr;?></span></td>
</tr>
<tr>
<td>Comment:</td>
<td colspan="2"><textarea rows="7" cols="30" name="5"></textarea></td>
<td> </td>
</tr>
<tr>
<td>Filename:</td>
<td colspan="2"><input type="text" name="6"></td>
<td><span class="error"><?php echo $fileErr;?></span></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit"> </td>
<td><input type="reset" name="reset" value="Reset" ></td>
<td> </td>
</tr>
</table>
</form>
<?php
$file = $_POST['6'];
$myfile = fopen($file, "a") or die("Unable to open file!");
$one = "\r\n" . $_POST['1'] . "\r\n";
fwrite($myfile, $one);
$two = $_POST['2'] . "\r\n";
fwrite($myfile, $two);
$three =$_POST['3'] . "\r\n";
fwrite($myfile, $three);
$four =$_POST['4'] . "\r\n";
fwrite($myfile, $four);
$five = $_POST['5'] . "\r\n";
fwrite($myfile, $five);
fclose($myfile);
?>
我的问题是:如何用表格中的空字段解决问题?
答案 0 :(得分:2)
您尝试在发送表单之前使用$_POST
。
<?php
if (!empty($_POST['6'])) {
$file = $_POST['6'];
$myfile = fopen($file, "a") or die("Unable to open file!");
$one = "\r\n" . $_POST['1'] . "\r\n";
fwrite($myfile, $one);
$two = $_POST['2'] . "\r\n";
fwrite($myfile, $two);
$three =$_POST['3'] . "\r\n";
fwrite($myfile, $three);
$four =$_POST['4'] . "\r\n";
fwrite($myfile, $four);
$five = $_POST['5'] . "\r\n";
fwrite($myfile, $five);
fclose($myfile);
}
?>
更好的方法是创建整个内容,然后将其放入文件中。
<?php
if (!empty($_POST['6'])) {
$file = $_POST['6'];
$myfile = fopen($file, "a");
$content = "\r\n" . $_POST['1'] . "\r\n" .
$_POST['2'] . "\r\n" .
$_POST['3'] . "\r\n" .
$_POST['4'] . "\r\n" .
$_POST['5'] . "\r\n";
fwrite($myfile, $content);
fclose($myfile);
}
?>