我有一个带有几个 html 表单的 php 页面。当文本字段为空时按下提交按钮时,我希望在页面刷新时出现错误,但我收到了 404 错误。奇怪的是,如果字段不为空,我不会得到 404。
这是 /books/index.php 和 404 错误状态的代码The requested URL /books/index.php was not found on this server.
<?php
//Books
//Variables on refresh:
// $_POST['pgNum'] //Page number
// $_POST['id'] //If page is being updated. ID of the book being updated
// $_POST['newBook'] //If new book is added. Name of the new book
// $_GET['empty'] //If the form to add a new book was submitted with 1 or more empty fields.
//Equals 1 if book name is empty, 2 if page number is empty, and 3 if both are empty.
// $_GET['emptyPgNum'] //If an update button was pressed with no page number in the corresponding field,
//this variable will exist on the redirect
require_once 'connection.php'; //Function to connect to MySQL database
require_once 'queryFunctions.php'; //Functions for sending MySQL queries
if(isset($_GET['emptyPgNum'])) {
//Reload from submission with an empty page number field
echo '<font color="red">Missing page number</font><br>';
}
//Check if this page loaded from a successful form submission
if(isset($_POST['id'])) { //If page update was submitted
//Check for empty form field
if(empty($_POST['pgNum'])) {
//Empty page number field. Redirect with error
unset($_POST);
header('Location: index.php?emptyPgNum=3');
}
//Update the pageNumber field and redirect back to index.php
$conn = getConnection();
$update = "UPDATE Books SET pageNumber={$_POST['pgNum']}
WHERE id={$_POST['id']}";
booleanQuery($conn, $update);
mysqli_close($conn);
header('Location: index.php');
} else if(isset($_POST['newBook'])) { //If new book is being added
//Check if any form fields were submitted empty
if(empty($_POST['newBook']) && empty($_POST['pgNum'])) {
//Both fields empty
unset($_POST);
header('Location: index.php?empty=3');
} else if(empty($_POST['newBook'])) {
//Empty book name
unset($_POST);
header('Location: index.php?empty=1');
} else if(empty($_POST['pgNum'])) {
//Empty page number
unset($_POST);
header('Location: index.php?empty=2');
}
//Insert the new book into the database and redirect back to index.php
$conn = getConnection();
$update = "INSERT INTO Books (name, pageNumber) VALUES(\"{$_POST['newBook']}\", {$_POST['pgNum']});";
booleanQuery($conn, $update);
mysqli_close($conn);
header('Location: index.php');
} else {
//No POST data
//HTML to add page title
?>
<head>
<title>Books</title>
</head>
<?php
//Retrieve the list of books
$conn = getConnection();
$query = 'SELECT * FROM Books ORDER BY id;';
$result = mysqli_query($conn, $query);
if(mysqli_errno($conn)) { //Display error if query failed
echo "<br>Query error:" . mysqli_error($conn) . "<br>" . mysqli_errno($conn) . "<br>";
exit();
}
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<body OnLoad="document.form1.pgNum.focus()"> <!--Set cursor focus to the first form field-->
<?php
//For each book in the database, show it's current page number and create a form for submitting a page update
$formNumber = 1;
while($row = mysqli_fetch_array($result)) {
echo "\"{$row['name']}\" page number: {$row['pageNumber']}<br>";
echo '<form action="index.php" method="post" name="form' . $formNumber . '">';
echo 'New Page Number: <input type="text" name="pgNum">';
echo '<input type="hidden" name="id" value="' . $row['id'] . '">';
echo '<input type="submit" value="Update">';
echo '</form><br>';
$formNumber++;
}
echo "<br>";
//Check if this page loaded from an unsuccessful form submission
if(isset($_GET['empty'])) {
//$_GET['empty'] equals 1 if book name is empty, 2 if page number is empty, and 3 if both are empty.
switch($_GET['empty']) {
case 1:
echo '<font color="red">Missing book name</font><br>';
break;
case 2:
echo '<font color="red">Missing page number</font><br>';
break;
case 3:
echo '<font color="red">Empty fields</font><br>';
break;
}
}
?>
<!--Create a form for adding a new book to the database-->
<form method="post">
New Book: <input type="text" name="newBook"><br>
Page Number: <input type="text" name="pgNum">
<input type="submit" value="Add">
</form>
</body>
</html>
编辑:我曾经在所有标题行中都有完全限定的 http:// 地址,但仍然有同样的错误。我在调试期间将它们更改为缩短的版本。