第一次发布海报,对mysqli来说很新,对不起,如果我的问题不对。 我有一张表(工作人员)的id,staffname和profile。 我希望能够通过从下拉列表中选择一名工作人员来编辑/添加记录,并在提交后转到名为add-edit-staff.php的新页面。
我创建了页面add-edit-staff.php,它完美无缺。如果我手动在浏览器的地址栏中键入“add-edit-staff.php”,表单就会出现添加新记录。如果我在浏览器的地址栏中手动输入“add-edit-staff.php?id = 1”,表单就会编辑id为1的记录。
现在我的问题是,我有一个名为view-staff.php的页面。此页面有一个带有下拉列表的表单,显示“staffmember”字段。我希望能够从该下拉列表中选择一条记录,并根据该记录的ID转到add-edit-staff.php页面上的相应记录。目前,当我选择我的记录时,它会直接回到mt add-edit-staff.php页面。这样做是因为view-staff.php页面没有在URL中发送记录的id。
我附上了我的代码,我很感激如何解决我的问题。正如我所说,我对此非常陌生,我一直在努力调整代码以适应我的目标,但我似乎无法理解它。
提前致谢..
添加 - 编辑staff.php
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<?php
// creates the new/edit record form
function renderForm($staffmember = '', $profile ='', $error = '', $id = '')
{
?>
<form action="" method="post">
<div>
<?php if ($id != '') { ?><input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php $id; ?><?php } ?>
<table width="675" border="0" cellspacing="10">
<tr>
<td valign="top"><strong>Staff Member</strong></td>
</tr>
<tr>
<td valign="top"><input name="staffmember" type="text" name="staffmember" size="147" value="<?php echo $staffmember; ?>"</td>
</tr>
<tr>
<td><strong>Staff Profile</strong></td>
</tr>
<tr>
<td><textarea name="profile" cols="110" rows="10"><?php echo $profile; ?></textarea></td>
</tr>
</table>
<p></p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body></html>
<?php }
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
// get variables from the URL/form
$id = $_POST['id'];
$staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
$profile = htmlentities($_POST['profile'], ENT_QUOTES);
// check that staffmember and profile are both not empty
if ($staffmember == '' || $profile == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($staffmember, $profile, $error, $id);
}
else
{
// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE staff SET staffmember = ?, profile = ?
WHERE id=?"))
{
$stmt->bind_param("ssi", $staffmember, $profile, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: view-staff.php");
}
}
// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// get 'id' from URL
$id = $_GET['id'];
// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM staff WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($id, $staffmember, $profile);
$stmt->fetch();
// show the form
renderForm($staffmember, $profile, NULL, $id);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view-staff2.php page
else
{
header("Location: view-staff.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
$profile = htmlentities($_POST['profile'], ENT_QUOTES);
// check that staffmember and profile are both not empty
if ($staffmember == '' || $profile == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($staffmember, $profile, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT staff (staffmember, profile) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $staffmember, $profile);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view-staff2.php");
}
}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
// close the mysqli connection
$mysqli->close();
?>
视图-staff.php
<form name="form1" method="post" action="add-edit-staff.php">
<select name="staffmember">
<?php
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM staff ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
{
while ($row = $result->fetch_assoc()) {
echo "<option value=\"{$row['id']}\">";
echo $row['staffmember'];
echo "</option>";
}
?>
<input type="submit" name="Submit" value=" Edit ">
</form<?php
}
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
?>
<a href="add-edit-staff.php"><h3 align="center">Add New Record</h3></a>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>
答案 0 :(得分:0)
使用此
更改您的view-staff.php<form name="form1" method="get" action="add-edit-staff.php">
<select name="id">
<?php
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM staff ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
{
while ($row = $result->fetch_assoc()) {
echo "<option value=\"{$row['id']}\">";
echo $row['staffmember'];
echo "</option>";
}
?>
<input type="submit" name="Submit" value=" Edit ">
</form<?php
}
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
?>
<a href="add-edit-staff.php"><h3 align="center">Add New Record</h3></a>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>
AND add-edit-staff.php
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<?php
// creates the new/edit record form
function renderForm($staffmember = '', $profile ='', $error = '', $id = '')
{
?>
<form action="" method="post">
<div>
<?php if ($id != '') { ?><input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php $id; ?><?php } ?>
<table width="675" border="0" cellspacing="10">
<tr>
<td valign="top"><strong>Staff Member</strong></td>
</tr>
<tr>
<td valign="top"><input name="staffmember" type="text" name="staffmember" size="147" value="<?php echo $staffmember; ?>"</td>
</tr>
<tr>
<td><strong>Staff Profile</strong></td>
</tr>
<tr>
<td><textarea name="profile" cols="110" rows="10"><?php echo $profile; ?></textarea></td>
</tr>
</table>
<p></p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body></html>
<?php }
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
// get variables from the URL/form
$id = $_POST['id'];
$staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
$profile = htmlentities($_POST['profile'], ENT_QUOTES);
if ($stmt = $mysqli->prepare("UPDATE staff SET staffmember = ?, profile = ?
WHERE id=?"))
{
$stmt->bind_param("ssi", $staffmember, $profile, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: view-staff.php");
}
// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// get 'id' from URL
$id = $_GET['id'];
// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM staff WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($id, $staffmember, $profile);
$stmt->fetch();
// show the form
renderForm($staffmember, $profile, NULL, $id);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view-staff2.php page
else
{
header("Location: view-staff.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
$profile = htmlentities($_POST['profile'], ENT_QUOTES);
// check that staffmember and profile are both not empty
if ($staffmember == '' || $profile == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($staffmember, $profile, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT staff (staffmember, profile) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $staffmember, $profile);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view-staff2.php");
}
}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
// close the mysqli connection
$mysqli->close();
?>