我有一个名为“add_order.php”的php文件。它需要三个输入,两个输入(Store和Customer)来自两个相应下拉列表的选择,最后一个输入(Date)来自用户的手动输入。 如果提交时日期保留为空,则页面会显示错误并在错误消息下方再次显示输入字段,以便用户重做输入。但是,当发生这种情况时,两个下拉列表会变为空白。有人可以帮忙吗? 谢谢。
<?php # add_order.php
$page_title = 'Add Order';
include ('mysqli_connect.php');
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {
$errors = array(); // Initialize error array.
// Check for a customer.
if (empty($_POST['Customer_ID'])) {
$errors[] = 'You forgot to enter the customer of the order.';
} else {
$customer_id = $_POST['Customer_ID'];
$query = "SELECT * FROM customers WHERE Customer_ID=$customer_id ORDER BY Last_N ASC";
$result = @mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
$customer_name=$row['First_N'].' '.$row['Last_N'];
}
}
// Check for a director ID.
if (empty($_POST['Store_ID'])) {
$errors[] = 'You forgot to enter the store of the order.';
} else {
$store_id = $_POST['Store_ID'];
$query = "SELECT * FROM store WHERE Store_ID=$store_id ORDER BY Store_Name ASC";
$result = @mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
$store_name=$row['Store_Name'];
}
}
// Check for the date of order.
if (empty($_POST['Order_Date'])) {
$errors[] = 'You forgot to enter the date of order.';
} else {
$order_date = $_POST['Order_Date'];
}
if (empty($errors)) { // If everything's okay.
// Add the order to the database.
// Make the query.
$query = "INSERT INTO prescription_orders (Customer_ID, Order_Date, Store_ID) VALUES ('$customer_id', '$order_date', '$store_id')";
$result = @mysqli_query ($dbc, $query); // Run the query.
if ($result) { // If it ran OK.
// Print a message.
echo '<h1 id="mainhead">Success!</h1>
<p>You have added:</p>';
echo "<table>
<tr><td>Customer_ID:</td><td>{$customer_id}</td></tr>
<tr><td>Order_Date:</td><td>{$order_date}</td></tr>
<tr><td>Store_ID:</td><td>{$store_id}</td></tr>
</table>";
$order_id = mysqli_insert_id($dbc); // Retrieve the id number of the newly added record
echo'<a href="http://localhost/cvs/add_order_item.php?id=' . $order_id . '">Add a item to this order</a>';
exit();
} else { // If it did not run OK.
echo '<h1 id="mainhead">System Error</h1>
<p class="error">The movie could not be added due to a system error. We apologize for any inconvenience.</p>'; // Public message.
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.
exit();
}
} else { // Report the errors.
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
} // End of if (empty($errors)) IF.
mysqli_close($dbc); // Close the database connection.
} // End of the main Submit conditional.
?>
<?php
if (isset($_POST['Customer_ID'])) $this_customer_id=$_POST['Customer_ID'];
if (isset($_POST['Store_ID'])) $this_store_id=$_POST['Store_ID'];
?>
<h2>Add Order</h2>
<form action="add_order.php" method="post">
<p>Store: <select name="Store_ID">
<?php
// Build the query
$query = "SELECT * FROM store ORDER BY Store_Name ASC";
$result = @mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
if ($row['Store_ID'] == $this_store_id) {
echo '<option value="'.$row['Store_ID'].'"
selected="selected">'.$row['Store_Name'].'</option>';
}
else {
echo'<option
value="'.$row['Store_ID'].'">'.$row['Store_Name'].'</option>';}
}
?>
</select> <a href="add_store.php">Add a new Store</a>
</p>
<p>Customer: <select name="Customer_ID">
<?php
// Build the query
$query = "SELECT * FROM customers ORDER BY Last_N ASC";
$result = @mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
if ($row['Customer_ID'] == $this_customer_id) {
echo '<option value="'.$row['Customer_ID'].'"
selected="selected">'.$row['First_N'].' '.$row['Last_N'].'</option>';
}
else {
echo'<option value="'.$row['Customer_ID'].'">'.$row['First_N'].'
'.$row['Last_N'].'</option>';}
}
?>
</select> <a href="add_customer.php">Add a new
Customer</a>
</p>
<p>Date of Order: <input type="text" name="Order_Date" size="10"
maxlength="10" value="<?php if (isset($_POST['Order_Date'])) echo
$_POST['Order_Date']; ?>" /> <i>YYYY-MM-DD</i></p>
<p><input type="submit" name="submit" value="Add Order" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<a href="index.php">Go back to Index Page</a>
答案 0 :(得分:0)
您正在关闭数据库连接,从而导致问题。
删除mysqli_close($dbc); // Close the database connection.
或者将其移到
下面 <a href="index.php">Go back to Index Page</a>
此外,您可以使用此:
<?= isset($_POST['Order_Date'])?$_POST['Order_Date']:'' ?>
作为此版本的缩短版本:
<?php if (isset($_POST['Order_Date'])) echo $_POST['Order_Date']; ?>