PHP表单,用于在表中显示时删除mySQLi中的记录

PHP表单,用于在表中显示时删除mySQLi中的记录,php,sql,mysqli,sql-delete,delete-row,Php,Sql,Mysqli,Sql Delete,Delete Row,我正在制作一个系统,在一个表中显示系统中的页面,其中一列是action列,它为用户提供了删除页面的选项。我这样做: listpages.php <?php $sql = "SELECT * FROM Pages"; $result = $conn->query($sql); if ($result) { while($row = $result->fetch_assoc()) { $pages[] = array_map('htmlspecialchar

我正在制作一个系统,在一个表中显示系统中的页面,其中一列是action列,它为用户提供了删除页面的选项。我这样做:

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


?>
<?php 

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

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

    $stmt = $conn->prepare("DELETE FROM Pages WHERE id = :id");
    $stmt->bindParam(":id", $target);

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

?>

您的
delpage.php
中有错误,您将错误的变量传递给sql查询id参数

调查一下这件事

试试这个:

delpage.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>
<?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();


?>
<?php 

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

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

    $stmt = $conn->prepare("DELETE FROM Pages WHERE id = :id");
    $stmt->bindParam(":id", $target);

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

?>

您的
delpage.php
中有错误,您将错误的变量传递给sql查询id参数

调查一下这件事

试试这个:

delpage.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>
<?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();


?>
<?php 

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

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

    $stmt = $conn->prepare("DELETE FROM Pages WHERE id = :id");
    $stmt->bindParam(":id", $target);

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

?>

如果这是一个大表单,那么您将有多个字段,所有字段都命名为targetid。这是一个问题,浏览器正在发送最后一个字段的值。尝试下面的小表单。每种形式都可以通过计算它们来拥有自己的名称

从上方移除表单并将其放置在循环中

<?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接收的具有相同名称和值的输入的简单示例:

<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';
}

总是第二次打印。
返回1
返回2

如果这是一个大表单,那么您将有多个字段,所有字段都命名为targetid。这是一个问题,浏览器正在发送最后一个字段的值。尝试下面的小表单。每种形式都可以通过计算它们来拥有自己的名称

从上方移除表单并将其放置在循环中

<?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接收的具有相同名称和值的输入的简单示例:

<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';
}

总是第二次打印。
返回1
返回2

读代码@user2860957读代码@user2860957