简体   繁体   English

PHP Mysql - 删除按钮继续删除最新行

[英]PHP Mysql - Delete button keeps on deleting latest row

When i run into a glitch, I always find find the answer on StackOverflow, but this time, although I'm sure the fix is easy, I just can't seem to get it right ! 当我遇到故障时,我总能在StackOverflow上找到答案,但这一次,虽然我确定修复很容易,但我似乎无法做到正确!

Basically, i'm trying to add a "delete" button next to each row fetched from my mysql database. 基本上,我正在尝试在从我的mysql数据库中提取的每一行旁边添加一个“删除”按钮。 The users should be able to delete a specific post, if needed. 如果需要,用户应该能够删除特定帖子。

When i hit the delete button, it's always the latest row that gets deleted. 当我点击删除按钮时,它总是被删除的最新行。 So i guess there's something wrong with the value passed in each row : seems like they're overridden by the latest one. 所以我猜每行传递的值有问题:看起来它们被最新的一个覆盖了。

Below's my code: 下面是我的代码:

<?php  
$table = query("SELECT post, postid FROM post_list WHERE id = ? ORDER BY 
time DESC LIMIT 15", $_SESSION["id"]);
foreach ($table as $row) 
{ 
      $post = $row["post"];
      $postid = $row["postid"];  
      echo ("<table>");
      echo ("<tr>");
      echo("<td>" . $post . "</td>");
      echo("</td>")?>            
      <div id="posteraser">
       <form action='' method='post'>
          <input type='hidden' name='postid' value='<?php echo $postid?>'>
          <input type='submit' name='posteraser'>Delete</input>
       </form>
      </div>       
     <?php 
     echo ("</td>");
     echo ("</tr>");
     echo ("</table>");
     echo '<hr>';
}  
?>

And below on the same page, there's the delete button code: 在同一页面下方,有删除按钮代码:

<?php
if(isset($_POST['posteraser']))
{
    $sql = query("DELETE FROM post_list WHERE postid = '$postid' ");       
    redirect ('home.php');
}
?>

Any help/tips will be much appreciated ! 任何帮助/提示将不胜感激! Thanks a lot ! 非常感谢 !

You have to pass here the $_POST['postid'] 你必须在这里传递$_POST['postid']

if(isset($_POST['posteraser'])){
   $postid = $_POST['postid'];
   $sql = query("DELETE FROM post_list WHERE postid = '$postid' ");       
   redirect ('home.php');
}

OR as procedure way 作为程序方式

$sql = query("DELETE FROM post_list WHERE postid = ? ",$postid);  

A developer should always be aware of the HTML code they create with their PHP code. 开发人员应始终了解他们使用PHP代码创建的HTML代码。
It's essential thing. 这是必不可少的。

As a matter of fact, HTML code is the very result of our efforts. 事实上,HTML代码是我们努力的结果。 NOT nice picture on can see in the browser windows - it's browser's job - but the very HTML code. 不是很好的图片可以在浏览器窗口中看到 - 这是浏览器的工作 - 但是HTML代码。

So, if you bother to see into generated code, you would discover something that can be boiled down to 所以,如果你懒得看到生成的代码,你会发现可以归结为的东西

<input type='hidden' name='postid' value='1'>
<input type='hidden' name='postid' value='3'>
<input type='hidden' name='postid' value='4'>
<input type='hidden' name='postid' value='5'>
<input type='hidden' name='postid' value='9'>

Do you have any questions why you have only last value? 你有什么问题为什么你只有最后的价值?

Speaking of solutions, you have two choices 说到解决方案,您有两种选择

  • create a separate form for the every row 为每一行创建一个单独的表单
  • mark the very Delete button with id. 用id标记删除按钮。

     <input type='submit' name='posteraser[<?php echo $postid?>]'>Delete</input> 

    for example 例如

let's check the logic from select statement. 让我们从select语句中检查逻辑。 you are selecting postid and assigning it to a hidden element and when you press delete button that hidden id is sent to server. 您正在选择postid并将其分配给隐藏元素,当您按下删除按钮时,隐藏ID将被发送到服务器。 so form creating under for loop is 所以在for循环下创建表单是

<div id="posteraser">
<form action='' method='post'>
  <input type='hidden' name='postid' value='<?php echo $postid?>'>
  <input type='submit' name='posteraser'>Delete</input>
</form>
</div>

but hidden element is creating with same name for each row. 但是隐藏元素为每一行创建了相同的名称。 so when you press delete button . 所以当你按删除按钮。 first hidden id is sent to server. 第一个隐藏的ID被发送到服务器。 and this hidden id is already newest as from your select statement. 这个隐藏的id已经是你的select语句中最新的了。

so what's the solution for it.. either you should sent postid through get attaching it in your url so that you can identify which delete button is pressed. 那么它的解决方案是什么..要么你应该发送postid通过将它附加到你的网址,以便你可以识别按下哪个删除按钮。 or create a logic to send only that id on which delete is pressed. 或创建一个逻辑,只发送按下删除的id。

This looks wrong: 这看起来不对:

echo ("<tr>");
echo("<td>" . $post . "</td>");
echo("</td>")?>     

The trailing </td> shouldn't be there. 尾随</td>不应该在那里。 Something else perhaps? 还有别的吗?

Also, you don't show how postid gets into $_SESSION['id'] 另外,你没有显示postid如何进入$_SESSION['id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM