如何在有多个post的页面中删除一个post? [MySql]

How to delete a post in a page with multiple posts? [MySql]

我想查询从页面中删除 post(thread) 但在该页面中我有多个 posts,我的数据库中有一个名为 [=27 的字段=] 是每个 post 的唯一 ID,但我在页面中列出了同一用户的多个 post,这是列出 post 的页面的代码:

<?
session_start();

  if(isset($_SESSION['id']))
  {
    $servername = "(...)";
    $username = "(...)";
    $password = "(...)";
    $dbname = "(...)";

    $connect  = mysqli_connect($servername, $username, $password, $dbname);

    if (!$connect) {
        die("Connection failed: " . mysqli_connect_error());
    }

}
else
{
  header(" url=index.php");
}

?>


(... hidded non relative html code ....)


<?
      $id = (isset($_GET['id'])) ? $_GET['id'] : $_SESSION['id'];

        $query = mysqli_query($connect,"SELECT * FROM thread inner join category on thread.id_type=category.id_type WHERE username = '".$id."'");

      echo '<div class="container marg-t-100">'."All posts from User: ".$id.'</div>';
      echo '<div class="container-fluid marg-t-25">';
      while($row = mysqli_fetch_array($query))
      {
      echo '<div class="col-md-1"></div>';
      echo '<div class="col-md-11">';
      echo '<form role="form" action="delete.php" method="post"><a type="submit" class="btn btn-danger marg-t-10 pull-right">Delete Thread</a></form>';
      echo  $row['title'] ."<br><br>".$row['content']."<br><br>".'<div class="pull-right">'. "date: ".$row['data']."<br>"."author: ".$row['username']."<br>".$row['name'].'</div>' ."<br><br><br><hr><br>";
      echo '</div>';
      }
      echo '</div>';
      mysqli_close($connect);
?>
  (...)

这是我正在谈论的页面的图片:

我希望当我单击 "Delete Thread" 时,相应的 post(线程)会从数据库中删除。

如果您在上面的页面代码中看到删除按钮在 delete.php 处起作用,这是我在该文件上的代码:

<?php
session_start();

  if(isset($_SESSION['id']))
  {
    $servername = "(...)";
    $username = "(...)";
    $password = "(...)";
    $dbname = "(...)";

    $connect  = mysqli_connect($servername, $username, $password, $dbname);

    $id = (isset($_GET['id'])) ? $_GET['id'] : $_SESSION['id'];

      $query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and id_thread ='".(I WANT THE RELATIVE ID HERE)."'");


    if (!$connect) {
        die("Connection failed: " . mysqli_connect_error());
    }

}
else
{
  header(" url=index.php");
}


 ?>

在列出用户 post 的页面中,在表单内添加一个隐藏字段,其值为 post ID 和 name 属性稍后您将在其中使用 POST 变量。

echo '<form role="form" action="delete.php" method="post"><button type="submit" class="btn btn-danger marg-t-10 pull-right" value="Delete Thread"><input type="hidden" name="thread_id" value="'.$row['id'].'"></form>';

并且在delete.php查询部分:

$query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and id_thread ='.$_POST['thread_id']);
<?
  echo '<form role="form" action="delete.php?id='.$row['thread_id'].'" method="post"><input type="submit" class="btn btn-danger marg-t-10 pull-right" value="Delete Thread"></form>';

delete.php

<?php
session_start();

if(isset($_SESSION['id']))
{
$servername = "(...)";
$username = "(...)";
$password = "(...)";
$dbname = "(...)";

$connect  = mysqli_connect($servername, $username, $password, $dbname);
$id=$_SESSION['id'];
$thread_id = $_GET['title'];

  $query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and thread_id ='".$thread_id."'");


if (!$connect) {
    die("Connection failed: " . mysqli_connect_error());
}

}
else
{
header(" url=index.php");
}


?>

希望这就是您所要求的。