Home > Back-end >  How to Echo delete link into php?
How to Echo delete link into php?

Time:07-24

I need to delete a row but I can't do it. Tell me please, how can this be done?

$id=0;
while ($row=$res->fetch_assoc())
{
$id  ;
echo "<tr>";
echo "<td>{$id}</td>";
echo "<td>{$row["BTITLE"]}</td>";
echo "<td>{$row["KEYWORDS"]}</td>";
echo "<td><a href='{$row["FILE"]}' 
target='_blank'>View</a></td>";
echo "<td><a href='view_books.php?id={$id}'>Delete</a></td>";
echo "</tr>";
}

I added

<?php
    session_start();
    include "database.php";
    if(!isset($_SESSION["AID"]))
    {
        $id=$_GET['id'];
        $delete=mysqli_query($conn, "DELETE FROM `book` WHERE `BID`='$id'");
        header("location:alogin.php");
        die();
    }
?>

Where is my mistake?

CodePudding user response:

Create a file delete.php that accept as parameter an id to delete. Then delete the row from table that corresponds to that id. I assume you can do it, if not, take some hints from file $row["FILE"].

Now your loop should look like:

while ($row=$res->fetch_assoc())
{
  $i  ;
  echo "<tr>";
  echo "<td>{$i}</td>";
  echo "<td>{$row["BTITLE"]}</td>";
  echo "<td>{$row["KEYWORDS"]}</td>";
  echo "<td><a href='{$row["FILE"]}' target='_blank'>View</a></td>";
  echo "<td><a href='delete.php?id={$i}'>Delete</a></td>";
  // OR, better if you know what it means:
  echo "<td><a href='delete.php?id={$row["id"]}'>Delete</a></td>";
  echo "</tr>";
}
  • Related