Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

How to delete a post which is belongs to the logged in user? In PHP

if a logged in user is having more than 1 post and when a user click on the particular post for delete it how to achieve it in php. my code query

$userId = $_SESSION['user_id'];
$sql = "SELECT thread_id FROM `threads` WHERE thread_user_id = $userId";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_assoc($result);

here is my thread link to delete it

  <p class="small mb-0" style="color: #aaa;">
     <?php echo $data['thread_id'] ?> //by this i am getting only 1st post id ,
// if i will use foreach loop to get all id but then i will get all the id on the same link 
    <a href="/thread?thread_id=<?php echo $data['thread_id'] ; ?>" class="link-grey">Remove</a>                                        
 </p>

when a user click on the remove link it should remove the specific post on which user is clicking based on that post id or something

0 likes
2 replies
MohamedTammam's avatar
$sql = "DELETE FROM `threads` WHERE thread_user_id = $userId and thread_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param($thread_id); // thread_id should be in $_POST['thread_id'] and you should use POST not GET for this action (use form instead of link)
$stmt->execute();
martinbean's avatar

@shivamyadav You don’t want to delete things using links like that. You also don’t want to just blindly pass using input to SQL statements like you are.

Why aren’t you using something like Laravel—that instills good practices such as the correct HTTP methods for deleting things, and automatically escaping SQL parameters—instead? You’re even asking on a Laravel-focused forum.

1 like

Please or to participate in this conversation.