PHP删除,编辑,保存帖子

时间:2017-11-22 19:38:09

标签: php post save edit

我查询了我的数据库,我的帖子ID如下:

<?php
//Database query code is here. 
$post_id = $row['id'];

?>

我已经为每个帖子添加了一个带按钮的表单,这样当单击时,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)。

但是我的代码无效。我的问题是form和action.php文件。我想在单击按钮时通过URL传递$ post_id作为函数的参数,但它不起作用。我是初学者,我花了好几个小时没有成功。

3 个答案:

答案 0 :(得分:1)

感谢@ 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

答案 1 :(得分:0)

你有三个问题:

  • 您正在拨打_$POST而不是 $_POST
  • 除上述内容外,您不应将美元符号传递到帖子中:例如,_$POST["$save_post"]应为$_POST["save_post"]
  • <input ... name = "$post_id">应该有一个正确的ID。你需要在这里实际输出 $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();

请注意,这三个函数都不需要任何函数参数,因为您在函数本身中定义了$post_id

希望这会有所帮助:)

答案 2 :(得分:0)

您必须从表单中接收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
    }
}