-1

I wanna make that only same user loged in and posted can delet the crud post how do i do that?

 <?php
    
    include("db.php");
    
    if(isset($_GET['id'])) {
      $id = $_GET['id'];
      $query = "DELETE FROM task WHERE id = $id";
      //$sql = "delete from tblusers WHERE id=:id and user_id=:user_id";
      $result = mysqli_query($conn, $query);
      if(!$result) {
        die("Query Failed.");
      }
    
      $_SESSION['message'] = 'Task Removed Successfully';
      $_SESSION['message_type'] = 'danger';
      header('Location: index.php');
    }
    
    ?>
ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • Hint: at the time of the deletion, do you know who created the item? – ndc85430 Jul 24 '22 at 17:02
  • should i use this? Alter table PRIMARY KEY (`id`), CONSTRAINT FK_products_1 FOREIGN KEY (login_id) REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE ) ENGINE=InnoDB; – Ahmed Agha Jul 24 '22 at 17:08
  • I added a relation between id in table and userid in but the code does not work $query = "delete from users WHERE id=:id and user_id=:user_id"; – Ahmed Agha Jul 25 '22 at 00:27
  • Welcome to SO! Your code is open to SQL Injection, so please consider reading the article https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php. Currently, you're only referring to IDs, but you can't rely on that they're not always just numbers (integers). – klediooo Jul 25 '22 at 11:39

1 Answers1

0

Creat a relation between userid in posts and id in username in mysql using drawing then use this code

$query = "DELETE FROM task WHERE id='$id' and userid = '$loggedin_session_id'";