php使用POST/GET from数据库编辑/添加新内容
为了帮助创建一个网站后端,我按照本教程->但是我的编辑按钮不能正常工作;相反,当用户在现有行上单击它,重定向到更新表单并输入值时,一个新行将添加到表中,而不是编辑现有行 我将编辑/添加php代码放在同一个文件中,如下所示:php使用POST/GET from数据库编辑/添加新内容,php,mysql,mysqli,Php,Mysql,Mysqli,为了帮助创建一个网站后端,我按照本教程->但是我的编辑按钮不能正常工作;相反,当用户在现有行上单击它,重定向到更新表单并输入值时,一个新行将添加到表中,而不是编辑现有行 我将编辑/添加php代码放在同一个文件中,如下所示: <?php /* Allows the user to both create new records and edit existing records */ //connect to database include ("newDBconn.php"); //c
<?php
/*
Allows the user to both create new records and edit existing records
*/
//connect to database
include ("newDBconn.php");
//creates the new/edit record form, with a function that makes it easily reusable since this form is used multiple times
function renderForm($first= '', $last='', $error = '', $groomingid = '')
{ ?>
<html>
<head>
<link href="adminnewstyle.css" rel="stylesheet" type="text/css" />
<title>
<h1><?php if ($groomingid != '') { echo "Edit Appointment"; }
else
{
echo "New Record"; }?>
</h1>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1><?php if ($groomingid != '') { echo "Edit Appointment"; } else { echo "New Record"; } ?> </h1>
<?php if ($error !='') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
."</div>";
} ?>
<form action="" method="post">
<div>
<?php if ($groomingid != '') { ?>
<input type="hidden" name="GroomingID" value="<?php echo $groomingid; ?>" />
<p>ID: <?php echo $GroomingID; ?> </p>
<?php } ?>
<strong>First Name: *</strong> <input type="text"
name="FirstName" value="<?php echo $first; ?>" />
<br />
<strong>Last Name: *</strong> <input type="text" name="LastName" value="<?php echo $last; ?>"/>
<p>*required</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['GroomingID']))
{
//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['GroomingID']))
{
//get variables from the URL/form
$groomingid = $_POST['GroomingID'];
$firstname = htmlentities($_POST['FirstName'], ENT_QUOTES);
$lastname = htmlentities($_POST['LastName'], ENT_QUOTES);
//check that firstname and lastname are both not empty
if ($firstname == '' || $lastname == '')
{
//if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($firstname, $lastname, $error, $groomingid);
}
else
{
//if everything is fine, update the record in the database
if($stmt = $mysqli->prepare("UPDATE grooming SET FirstName = ?, LastName = ? WHERE GroomingID=?"))
{
$stmt->bind_param("ssi", $firstname, $lastname, $groomingid);
$stmt->execute();
$stmt->close();
}
//show an error message if the query encounters an error
else
{
echo "Error: could not prepare sql statement.";
}
//redirect the user once the form is updated
header("Location: PS_Manage_Appnts.php");
exit();
}
}
//if the 'id' variable isn't valid, show 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['GroomingID']) && $_GET['GroomingID'] > 0)
{
//get 'id' from URL
$id = $_GET['GroomingID'];
//get the record from the database
if($stmt = $mysqli->prepare("SELECT * FROM grooming WHERE GroomingID=?"))
{
$stmt->bind_param("i", $groomingid);
$stmt->execute();
$stmt->bind_result($groomingid, $firstname, $lastname);
$stmt->fetch();
//show the form
renderForm($firstname, $lastname, NULL, $groomingid);
$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 PS_Manage_Appnts.php page
else
{
header("location:PS_Manage_Appnts.php");
exit();
}
}
}
/* 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
$firstname = htmlentities($_POST['FirstName'], ENT_QUOTES);
$lastname = htmlentities($_POST['LastName'], ENT_QUOTES);
//check that firstname and lastname are both not empty
if ($firstname == '' || $lastname == '')
{
//if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($firstname, $lastname, $error);
}
else
{
//insert the new record into the database
if($stmt = $mysqli->prepare("INSERT grooming (FirstName, LastName) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $firstname, $lastname);
$stmt->execute();
$stmt->close();
}
//show an error if the query has an error
else
{
echo "Error: could not prepare sql statement.";
}
//redirect the user
header ("location:PS_Manage_Appnts.php");
exit();
}
}
//if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
//close the connection
$mysqli->close();
?>
你的问题是:
if (isset($_GET['GroomingID']))
{
//if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
当您的表单发布到同一页面时(无需添加查询变量GroomingID
),发布编辑过的表单时,第一个条件的计算结果为false
您应该检查POST或获取:
if (isset($_GET['GroomingID']) || isset($_POST['GroomingID']))
或:
您正在检查通过GET输入的参数GroomingID
,但事实并非如此,因为它位于带有方法POST的表单中的隐藏字段中。@CBroe您是建议我从特定输入中删除“hidden”属性还是…?否,无论输入字段是隐藏的类型
还是任何其他类型,都不会改变表单用于发送数据的方法…@CBroe那么,你是说如果我将方法
更改为“get”而不是“post”,这将解决我的问题吗?不,不会,因为在剩下的代码中,您希望通过POST再次发送参数…解决问题的方法是进行适当的调试,查看脚本实际接收到哪些参数,以及if/else分支到哪个。好的,我将if(isset($\u GET['GroomingID'))
更改为if(isset($\u GET['GroomingID'))| | isset($_POST['GroomingID'))
我仍然有同样的问题。
if (isset($_REQUEST['GroomingID'])) // assuming there are no cookies with the same name