0

I want to make a query that deletes a post(thread) from a page but in that page I have multiple posts, I have in my database a field called id_thread is the unique id of every post but I'm listing multiple posts of the same user in the page, this is the code of the page where the posts are listed:

<?
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);
?>
  (...)

this is an image of the page im talking about:

enter image description here

I want that when I click "Delete Thread" the respective post(thread) gets deleted from database.

If you see in the page code above you can see that the delete button takes action at delete.php, this is my code on that file:

<?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");
}


 ?>
kraven2g
  • 167
  • 1
  • 16

2 Answers2

1

In your page that lists user's posts, inside the form add a hidden field with a value of the post ID and a name attribute in which you will use later in your POST variables.

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>';

And in delete.php query part:

$query = mysqli_query($connect,"Delete FROM thread WHERE username = '".$id."' and id_thread ='.$_POST['thread_id']);
0
<?
  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");
}


?>

I hope this is what you asked for.

  • hi thank you for the time writing this, but after changing my code I click in the btn and nothing happends – kraven2g Jul 03 '16 at 16:48