kraven2g kraven2g - 5 months ago 8
HTML Question

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

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


?>

Answer

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']);
Comments