PHP单击按钮从表中删除行

我在传递按钮的ID以根据ID删除行时遇到问题。我该怎么做才能正确传递ID?


   <form method="POST" >

                <table border="1">

                    <tr>

                        <th>Student Name</th>

                        <th>Matric Number</th>

                        <th>IC Number</th>

                        <th></th>

                        <th></th>

                    </tr>

                    <?php

                        $link=mysqli_connect("localhost","root","") or die(mysqli_error());

                        mysqli_select_db($link,"myDataBase") or die(mysqli_error());

                        $query="Select * From student" or die(mysqli_error());

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


                        if($result->num_rows > 0) {

                        while($row = $result->fetch_assoc()) {

                            echo "<tr><td>".$row["name"]."</td><td>".$row["matric"]."</td><td>".$row["ic"]."</td>

                            <td><input type=button value=Update></td><td><input type=submit value=Delete name=delete ><input type=hidden name=id value=".$row["id"]." ></td></tr>";     

                        }   

                        }else{  

                            die("0 results");

                        }

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

                            echo $did=$_POST['id'];

                            $query="Delete From student where id='$did'";

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

                        }

                    ?>

                </table>

            </form>


胡子哥哥
浏览 389回答 2
2回答

至尊宝的传说

问题在于,所有包含学生ID的隐藏字段都放在一种表单中。因此,ID当您单击任何删除按钮时,都会发布最后一个隐藏字段。将form标签分别放置Delete在每一行的列内,然后仅发布单击的行ID。还要在SELECT查询后放置查询,DELETE以在删除后立即刷新HTML表。您还需要避免SQL注入。<?php$link = mysqli_connect( "localhost", "root", "" ) or die( mysqli_error() );mysqli_select_db( $link, "myDataBase" ) or die( mysqli_error() );// delete recordif( isset( $_POST['delete'] ) ) {&nbsp; &nbsp; echo $did = $_POST['id'];&nbsp; &nbsp; $query = $link->prepare( "DELETE FROM student WHERE id=?" );&nbsp; &nbsp; $query->bind_param( "s", $did );&nbsp; &nbsp; $query->execute();}// get all records$query = "SELECT * FROM student" or die( mysqli_error() );$result = mysqli_query( $link, $query );?><table border="1">&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>Student Name</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Matric Number</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>IC Number</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Update</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Delete</th>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; if( $result->num_rows > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while( $row = $result->fetch_assoc() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["name"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["matric"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["ic"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td><input type=button value=Update></td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td><form method='POST'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type=hidden name=id value=".$row["id"]." >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type=submit value=Delete name=delete >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die("0 results");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; ?></table>您还可以创建删除链接(即test.php的?delete_id = 100)为每一行单独而不是创建的form和GETID删除服务器端。

HUH函数

您需要更改查询:&nbsp;$query="Delete&nbsp;From&nbsp;student&nbsp;where&nbsp;id=$did";反而&nbsp;$query="Delete&nbsp;From&nbsp;student&nbsp;where&nbsp;id='$did'";
打开App,查看更多内容
随时随地看视频慕课网APP