PHP表单,用于在显示后在表中删除mySQLi中的记录 [英] PHP Form to delete records in mySQLi when displaying then in a table

查看:80
本文介绍了PHP表单,用于在显示后在表中删除mySQLi中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个系统,以便在表格中显示系统中的页面,其中一列是action列,使人们可以选择删除页面。我是这样做的:

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();


?>

这部分起作用:我得到了设置为删除记录的垃圾桶图标。问题是,当我单击它时,它将始终删除最后一条记录,而不是说要删除的记录。这是一个问题,而不是它应该如何工作。我看不到为什么要这样做,因为ID与其他信息一样被显示。我在SO中寻找答案,但没有任何效果,除了创建问题之外,我没有看到很多其他选择。有帮助吗?

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?

预先感谢

推荐答案

如果这是一个大表格,那么您将有多个字段都命名为targetid。这是一个问题,浏览器正在发送最后一个字段的值。尝试如下所示的小表格。每个表格都可以通过编号来命名。

If this is one big form then you will have multiple fields all named targetid. That's a problem and the browser is sending the value of the last field. Try small forms like below. Each form could have it's own name by numerating them.

从窗体上方的循环中删除该窗体。

Remove the form from above an place it in the loop.

<?php foreach($pages as $page):
?>
<tr>
<form method="post" action="utils/delpage.php">
    <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>
</form>
<?php endforeach ?>

这是一个简单的示例,其输入内容与PHP接收到的名称和值相同:

Here is a simple example of inputs with the same name and the value that is received by PHP:

<form method="post" action="#">
    <input name="a" value="1">
    <input name="a" value="2">
    <button type="submit" value="delete">Always prints second.</button>
</form>

<form method="post" action="#">
    <input name="a" value="1">
    <button type="submit" value="delete">Returns 1</button>
</form>
    <form method="post" action="#">
    <input name="a" value="2">
    <button type="submit" value="delete">Returns 2</button>
</form>


<?php
if (isset($_POST['a'])) {
    echo $_POST['a'];
} else {
    echo 'page loaded';
}

这篇关于PHP表单,用于在显示后在表中删除mySQLi中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆