Specifying buttons related to an already inserted data (edit, delete, print, like, ..) PHP & MySQL

Viewed 8

I get the inserted data to appear on the screen, now I want to allow the user to edit, delete, print, upvote & comment on this piece of data (products, posts, or any kind of data..). I know how to code that, but the problem is how can I make the delete button for instance take the id of this product, while there's a list of them and every delete button should delete its product? How to link such services related to every product with the id of the product itself?

let's take a very simple example just to understand the matter:

  <form action="" method="post">
    <input type="text" name="name" placeholder="Product Name">
    <input type="text" name="content" placeholder="Product Content"> 
    <br><br>
    <button type="submit" name="submit">Submit</button>
  </form>

the MySQL simple table:

CREATE TABLE `products` (
  `id` int not null auto_increment,
  `name` varchar(40) not null,
  `content` varchar(100) not null,
  PRIMARY KEY(`id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

inside the same page the PHP code to insert into DB and show products and services(edit, delete), discarding protection, escaping or closing connections:

<?php
  // Submit data
  if(isset($_POST['submit'])){
    $name = $_POST['name']; 
    $content= $_POST['content']; 
    $sql = "INSERT INTO products (name, content) VALUES ('$name', '$content');";
    mysqli_query($conn, $sql);

  
    // Display data
    $name = $_POST['name']; 
    $content= $_POST['content']; 
    $sql = "SELECT * FROM products";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($result)){ 
      echo '<b>Name: </b>' . $row['name'] . '<br>' . 
           '<b>content: </b>' . $row['content'] . '<br><br>';
      echo '
      <button name="editName">Edit Name</button>
      <button name="editContent">Edit content</button>
      <button name="delete">Delete</button>';
    }
  }
?>

it is supposed when toggling the delete button, it has to delete the specific product related to it!

0 Answers
Related