尝试在 PHP 中为每一行构建一个删除按钮,但出现错误

我正在尝试在 PHP 中使用以下代码来生成行和删除按钮。但不是删除选定的,而是从行的底部删除。


<?php

$connection = mysqli_connect('localhost','root','','rohit');

{


    $query  = "SELECT * FROM user";

    $result = mysqli_query($connection,$query);


    while($row = mysqli_fetch_assoc($result)) {

        $id     = $row['id'];

        $name   = $row['name'];

        $lname  = $row['lname'];

        $dept   = $row['department'];

        $dob    = $row['DOB'];

        $doj    = $row['DOJ'];

        $mobile = $row['mobile'];

        $email  = $row['email'];

        $salary = $row['salary'];

        $gender = $row['gender'];



        echo "<tr>";

        echo "<td>{$id}</td>";

        echo "<td>{$name}</td>";

        echo "<td>{$lname}</td>";

        echo "<td>{$dept}</td>";

        echo "<td>{$dob}</td>";

        echo "<td>{$doj}</td>";

        echo "<td>{$mobile}</td>";

        echo "<td>{$email}</td>";

        echo "<td>{$salary}</td>";

        echo "<td>{$gender}</td>";

        echo '<td>' . '<form method="post"><button class="mb-1 mr-1 btn btn-primary" name="edit"><i class="fa fa-edit"></i></button>' .

            '<button class="mb-1 mr-1 btn btn-danger" name="delete"><i class="fa fa-times"></i></button></form>'

            . '</td>';

        echo "</tr>";


    }

}


?>

<?php

if (isset($_POST['delete'])) {

    $query1  = "DELETE FROM user ";

    $query1 .= "WHERE id = $id LIMIT 1";

    $result1 = mysqli_query($connection,$query1);


    if (!$result1) {

        die("FAILED" . mysqli_error($connection));

    }

}

?>


任何形式的帮助是appreaciated 。提前致谢。:)


精慕HU
浏览 274回答 3
3回答

温温酱

在您的表单中,您需要使用要删除的行的id传递action属性所以它会是这样的<form method="post" action="<?= $_SERVER['PHP_SELF']?>?id={$id}">然后在你的 SQL 查询中,你将传递这个 id,我在这里使用准备好的语句来保护你免受 SQL 注入<?php&nbsp; if (isset($_POST['delete'])) {&nbsp; &nbsp; &nbsp;$stmt = $connection->prepare("DELETE FROM user WHERE id=?");&nbsp; &nbsp; &nbsp;$stmt->bind_param($_GET['id']);&nbsp; &nbsp; &nbsp;$stmt->execute();&nbsp; &nbsp;if (!$result1) {&nbsp; &nbsp; die("FAILED" . mysqli_error($connection));&nbsp; &nbsp;}&nbsp;}?>

牛魔王的故事

您正在使用$id用于$result在 while 循环中从对象获取数据的。循环在数据库中最后一个当前 id 处结束,因此它只会删除最后一个$id并且只删除一次。您可以$id在表格中写下每条记录。例如:<form method="post"><button class="mb-1 mr-1 btn btn-primary" name="edit"><i class="fa fa-edit"></i></button>' .&nbsp; &nbsp; '<input display="hidden" name="id" value="'.$id.'">'.&nbsp; &nbsp; '<button class="mb-1 mr-1 btn btn-danger" name="delete"><i class="fa fa-times"></i></button></form>然后在你的 PHP 中if (isset($_POST['delete'])) {&nbsp; &nbsp; $d_id = $_POST['id'];&nbsp; &nbsp; $query1&nbsp; = "DELETE FROM user ";&nbsp; &nbsp; $query1 .= "WHERE id = $d_id LIMIT 1";&nbsp; &nbsp; $result1 = mysqli_query($connection,$query1);}

三国纷争

我对您的代码进行了一些更改。这会起作用..!!1.更改方法属性(给定表单方法 POST ,但在 php 查询中它是 GET。现在都是 POST 类型)2.删除时将id作为隐藏字段传递。所以它会删除,正确的记录。此外删除查询是更改(使用此 ID 删除记录)<?php$connection = mysqli_connect('localhost','root','','rohit');{$query&nbsp; = "SELECT * FROM user";$result = mysqli_query($connection,$query);while($row = mysqli_fetch_assoc($result)) {&nbsp; &nbsp; $id&nbsp; &nbsp; &nbsp;= $row['id'];&nbsp; &nbsp; $name&nbsp; &nbsp;= $row['name'];&nbsp; &nbsp; $lname&nbsp; = $row['lname'];&nbsp; &nbsp; $dept&nbsp; &nbsp;= $row['department'];&nbsp; &nbsp; $dob&nbsp; &nbsp; = $row['DOB'];&nbsp; &nbsp; $doj&nbsp; &nbsp; = $row['DOJ'];&nbsp; &nbsp; $mobile = $row['mobile'];&nbsp; &nbsp; $email&nbsp; = $row['email'];&nbsp; &nbsp; $salary = $row['salary'];&nbsp; &nbsp; $gender = $row['gender'];&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; echo "<td>{$id}</td>";&nbsp; &nbsp; echo "<td>{$name}</td>";&nbsp; &nbsp; echo "<td>{$lname}</td>";&nbsp; &nbsp; echo "<td>{$dept}</td>";&nbsp; &nbsp; echo "<td>{$dob}</td>";&nbsp; &nbsp; echo "<td>{$doj}</td>";&nbsp; &nbsp; echo "<td>{$mobile}</td>";&nbsp; &nbsp; echo "<td>{$email}</td>";&nbsp; &nbsp; echo "<td>{$salary}</td>";&nbsp; &nbsp; echo "<td>{$gender}</td>";&nbsp; &nbsp; echo '<td>' . '<form method="post"><button class="mb-1 mr-1 btn btn-primary" name="edit"><i class="fa fa-edit"></i></button>' .&nbsp; &nbsp; &nbsp; &nbsp; '<input type="hidden" name="del_id" value="'.$id.'">'.&nbsp; &nbsp; &nbsp; &nbsp; '<button class="mb-1 mr-1 btn btn-danger" name="delete"><i class="fa fa-times"></i></button></form>'&nbsp; &nbsp; &nbsp; &nbsp; . '</td>';&nbsp; &nbsp; echo "</tr>";}}?><?phpif (isset($_POST['delete'])) {$del_id = $_POST['del_id'];$query1&nbsp; = "DELETE FROM user ";$query1 .= "WHERE id = $del_id LIMIT 1";$result1 = mysqli_query($connection,$query1);if (!$result1) {&nbsp; &nbsp; die("FAILED" . mysqli_error($connection));}}?>
打开App,查看更多内容
随时随地看视频慕课网APP