PHP Form to delete records in mySQLi when displaying then in a table

I’m making a system to display pages in the system in a table and one of the columns is the action column giving people the option to delete the page. I did this:

listpages.php

<?php
$sql = "SELECT * FROM Pages";
$result = $conn->query($sql);
if ($result) {
    while($row = $result->fetch_assoc()) {
        $pages[] = array_map('htmlspecialchars', $row);
    }
}
// PHP is finished now, here's the HTML
?>
      <form method="post" action="utils/delpage.php">
      <table>
        <thead>
          <tr>
              <th>Page Name</th>
              <th>Page Type</th>
              <th>Registered on</th>
              <th>Actions</th>
          </tr>
        </thead>
        <tbody>
        <?php foreach($pages as $page):?>
          <tr>
          <input type="hidden" name="targetid" value="<?= $page['id'] ?>"></input>
            <td><?= $page['pagename'] ?></td>
            <td><?= $page['pagetype'] ?></td>
            <td><?= $page['regdate'] ?></td>
            <td><input type="submit" value="delete" class="red-text material-icons tooltipped" data-tooltip="Delete"><td>
          </tr>
        <?php endforeach ?>
        </tbody>
      </table>
      </form>

delpage.php

<?php 

include ('../../php/db.php');


$id = $_POST['targetid'];
$target = htmlentities($id);

$stmt = $conn->prepare("DELETE FROM Pages WHERE id = (?)");
$stmt->bind_param("i", $page);

$page = $target;
$stmt->execute();


?>

This partially works: I get the trash icon I set to delete records. The problem is, when I click it, it will always delete the last record instead of the record it was said to delete. This is a problem, an not how it was really supposed to work. I don’t see why it’s doing this as the id is being “displayed” the same way the other information is. I searched SO for an answer but nothing worked and I didn’t see many options but creating a question. Any help?

Thanks in advance


Source: stackoverflow-php