PHP 删除、编辑、保存 Post
PHP Delete, Edit, Save Post
我查询了我的数据库,我有一个 post 的 ID,如下所示:
<?php
//Database query code is here.
$post_id = $row['id'];
?>
我已经为每个 post 添加了一个带有按钮的表单,以便在单击时,将执行 action.php 文件中的相应函数以输出字符串。
如下
<form action = "action.php" method = "post">
<input type = "hidden" name = <?php echo "$post_id"?>">
<button type = "submit" name = "delete_post">Delete post </button>
<button type = "submit" name = "edit_post">Edit post </button>
<button type = "submit" name = "save_post">Save post </button>
</form>
我有一个 PHP 文件,其中包含如下几个函数
action.php
<?php
//first function
function delete_post($post_id){
if(isset($_POST["delete_post"])){
$post_id = $_POST["$post_id"];
echo " you attempted to delete the post, baby";
}
}
//Second function
function edit_post($post_id){
if(isset($_POST["edit_post"])){
$post_id = $_POST["$post_id"];
echo "Hey you attempted to edit the post!";
}
}
//Third function
function save_post($post_id){
if(isset($_POST["save_post"])){
$post_id = $_POST["$post_id"];
echo " Wow, did you really need to save that post?";
}
}
delete_post($post_id);
edit_post($post_id);
save_post($post_id);
?>
我想实现的是,比如点击delete_post按钮时,只有
应该执行函数 delete_post($post_id)。
如果单击 edit_post 按钮,仅应执行函数 edit_post($post_id)。
如果单击 save_post 按钮,仅应执行函数 save_post($post_id)。
但是我的代码不起作用。我的问题是表格和 action.php 文件。我想在单击按钮时通过 URL 将 $post_id 作为函数的参数传递,但它不起作用。我是初学者,我花了好几个小时都没有成功。
感谢@chris85 鼓励重新阅读问题。
输入表单及支持表单的函数如下
input_form.php
<html>
<body>
<form action="action.php" method="post">
<?php echo '<input type="hidden" name="post_id" value="$post_id">' ?>
<input type="radio" name="action" value="delete_post">Delete
<br>
<input type="radio" name="action" value="edit_post">Edit
<br>
<input type="radio" name="action" value="save_post">Save
<br>
<input type="submit">
</form>
</body>
</html>
action.php
<?php
if ( ! isset($_POST['post_id']) ) {
// Handle the case if $_POST['post_id'] is not set
}
if ( ! isset($_POST['action']) ) {
// Handle the case if $_POST['action'] is not set
}
# Print the input we got
echo $_POST['post_id'];
echo '<br>';
echo $_POST['action'];
echo '<br>';
if ( $_POST['action'] === 'delete_post' ) {
delete_post($_POST['post_id']);
}
elseif ( $_POST['action'] === 'edit_post' ) {
edit_post($_POST['post_id']);
}
elseif ( $_POST['action'] === 'save_post' ) {
save_post($_POST['post_id']);
}
//first function
function delete_post($post_id){
echo " you attempted to delete the post, baby";
}
//Second function
function edit_post($post_id){
echo "Hey you attempted to edit the post!";
}
//Third function
function save_post($post_id){
echo " Wow, did you really need to save that post?";
}
?>
希望对您有所帮助!
在您的原始代码中,每种操作类型都有一个单独的按钮(即一个用于删除、编辑和保存)。如果你强烈希望有多个按钮而不是单选按钮,我会建议以下 JavaScript 答案:Pass a hidden field value based on which button is clicked with JavaScript
您遇到三个问题:
- 您正在呼叫
_$POST
而不是 $_POST
.
- 除上述之外,您不应该将美元符号传递到 post 中:
_$POST["$save_post"]
例如应该是 $_POST["save_post"]
。
<input ... name = "$post_id">
应该有一个正确的 ID。您实际上需要在此处 output $post_id
,而不是只写出 PHP 变量。记住;这是原始的 HTML;您不在 PHP 块内,因此您的 ID 实际上只是字符串 $post_id
。要更正此问题,请使用 <input ... name = "<?php echo $post_id; ?>">
. 输出变量
- 你实际上并没有唤起你的任何功能;您需要将代码放在函数之外,以便三个条件都可以独立触发:
if(isset($_POST["delete_post"])){
$post_id = $_POST["post_id"];
echo " you attempted to delete the post, baby";
}
if(isset($_POST["edit_post"])){
$post_id = $_POST["post_id"];
echo "Hey you attempted to edit the post!";
}
if(isset($_POST["save_post"])){
$post_id = $_POST["post_id"];
echo " Wow, did you really need to save that post?";
}
或者确保在页面加载时调用所有三个函数:
function delete_post(){ ... }
function edit_post(){ ... }
function save_post(){ ... }
delete_post();
edit_post();
save_post();
请注意,这三个函数中的 none 需要任何函数参数,因为您在函数本身内部定义 $post_id
。
希望这对您有所帮助:)
您必须从表单接收 itens,并执行 if 语句。
<form action = "action.php?post_id=$post_id" method = "post">
<input type="hidden" name = "$post_id">
<button type="submit" name="delete_post" value="delete_post">Delete post </button>
<button type="submit" name="edit_post" value="edit_post">Edit post </button>
<button type="submit" name="save_post" value="save_post">Save post </button>
</form>
而在PHP中,是这样的(注意,一定要防止SQL注入!!!):
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
if (isset($_POST['delete_post'])) {
// delete_post code
} else if (isset($_POST['edit_post'])) {
//edit_post code
} else {
// save_post code
}
}
我查询了我的数据库,我有一个 post 的 ID,如下所示:
<?php
//Database query code is here.
$post_id = $row['id'];
?>
我已经为每个 post 添加了一个带有按钮的表单,以便在单击时,将执行 action.php 文件中的相应函数以输出字符串。 如下
<form action = "action.php" method = "post">
<input type = "hidden" name = <?php echo "$post_id"?>">
<button type = "submit" name = "delete_post">Delete post </button>
<button type = "submit" name = "edit_post">Edit post </button>
<button type = "submit" name = "save_post">Save post </button>
</form>
我有一个 PHP 文件,其中包含如下几个函数
action.php
<?php
//first function
function delete_post($post_id){
if(isset($_POST["delete_post"])){
$post_id = $_POST["$post_id"];
echo " you attempted to delete the post, baby";
}
}
//Second function
function edit_post($post_id){
if(isset($_POST["edit_post"])){
$post_id = $_POST["$post_id"];
echo "Hey you attempted to edit the post!";
}
}
//Third function
function save_post($post_id){
if(isset($_POST["save_post"])){
$post_id = $_POST["$post_id"];
echo " Wow, did you really need to save that post?";
}
}
delete_post($post_id);
edit_post($post_id);
save_post($post_id);
?>
我想实现的是,比如点击delete_post按钮时,只有 应该执行函数 delete_post($post_id)。
如果单击 edit_post 按钮,仅应执行函数 edit_post($post_id)。
如果单击 save_post 按钮,仅应执行函数 save_post($post_id)。
但是我的代码不起作用。我的问题是表格和 action.php 文件。我想在单击按钮时通过 URL 将 $post_id 作为函数的参数传递,但它不起作用。我是初学者,我花了好几个小时都没有成功。
感谢@chris85 鼓励重新阅读问题。
输入表单及支持表单的函数如下
input_form.php
<html>
<body>
<form action="action.php" method="post">
<?php echo '<input type="hidden" name="post_id" value="$post_id">' ?>
<input type="radio" name="action" value="delete_post">Delete
<br>
<input type="radio" name="action" value="edit_post">Edit
<br>
<input type="radio" name="action" value="save_post">Save
<br>
<input type="submit">
</form>
</body>
</html>
action.php
<?php
if ( ! isset($_POST['post_id']) ) {
// Handle the case if $_POST['post_id'] is not set
}
if ( ! isset($_POST['action']) ) {
// Handle the case if $_POST['action'] is not set
}
# Print the input we got
echo $_POST['post_id'];
echo '<br>';
echo $_POST['action'];
echo '<br>';
if ( $_POST['action'] === 'delete_post' ) {
delete_post($_POST['post_id']);
}
elseif ( $_POST['action'] === 'edit_post' ) {
edit_post($_POST['post_id']);
}
elseif ( $_POST['action'] === 'save_post' ) {
save_post($_POST['post_id']);
}
//first function
function delete_post($post_id){
echo " you attempted to delete the post, baby";
}
//Second function
function edit_post($post_id){
echo "Hey you attempted to edit the post!";
}
//Third function
function save_post($post_id){
echo " Wow, did you really need to save that post?";
}
?>
希望对您有所帮助!
在您的原始代码中,每种操作类型都有一个单独的按钮(即一个用于删除、编辑和保存)。如果你强烈希望有多个按钮而不是单选按钮,我会建议以下 JavaScript 答案:Pass a hidden field value based on which button is clicked with JavaScript
您遇到三个问题:
- 您正在呼叫
_$POST
而不是$_POST
. - 除上述之外,您不应该将美元符号传递到 post 中:
_$POST["$save_post"]
例如应该是$_POST["save_post"]
。 <input ... name = "$post_id">
应该有一个正确的 ID。您实际上需要在此处 output$post_id
,而不是只写出 PHP 变量。记住;这是原始的 HTML;您不在 PHP 块内,因此您的 ID 实际上只是字符串$post_id
。要更正此问题,请使用<input ... name = "<?php echo $post_id; ?>">
. 输出变量
- 你实际上并没有唤起你的任何功能;您需要将代码放在函数之外,以便三个条件都可以独立触发:
if(isset($_POST["delete_post"])){
$post_id = $_POST["post_id"];
echo " you attempted to delete the post, baby";
}
if(isset($_POST["edit_post"])){
$post_id = $_POST["post_id"];
echo "Hey you attempted to edit the post!";
}
if(isset($_POST["save_post"])){
$post_id = $_POST["post_id"];
echo " Wow, did you really need to save that post?";
}
或者确保在页面加载时调用所有三个函数:
function delete_post(){ ... }
function edit_post(){ ... }
function save_post(){ ... }
delete_post();
edit_post();
save_post();
请注意,这三个函数中的 none 需要任何函数参数,因为您在函数本身内部定义 $post_id
。
希望这对您有所帮助:)
您必须从表单接收 itens,并执行 if 语句。
<form action = "action.php?post_id=$post_id" method = "post">
<input type="hidden" name = "$post_id">
<button type="submit" name="delete_post" value="delete_post">Delete post </button>
<button type="submit" name="edit_post" value="edit_post">Edit post </button>
<button type="submit" name="save_post" value="save_post">Save post </button>
</form>
而在PHP中,是这样的(注意,一定要防止SQL注入!!!):
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
if (isset($_POST['delete_post'])) {
// delete_post code
} else if (isset($_POST['edit_post'])) {
//edit_post code
} else {
// save_post code
}
}