简体   繁体   中英

PHP delete row from a table with button click

I'm having problem to pass the id of the button to delete the row according to the id. What should i do to pass the id correctly?

   <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>

You need to change your query:

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

instead

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

Problem is that all hidden fields containing student IDs are placed inside one form. Therefor always last hidden field ID is posted when you click any delete button. Place your form tag inside Delete column for each row separately then only clicked row ID will be posted. Also place your SELECT query after DELETE query to refresh your HTML table immediately after delete. You also need to avoid SQL injection.

<?php

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

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

    echo $did = $_POST['id'];
    $query = $link->prepare( "DELETE FROM student WHERE id=?" );
    $query->bind_param( "s", $did );
    $query->execute();
}

// get all records
$query = "SELECT * FROM student" or die( mysqli_error() );
$result = mysqli_query( $link, $query );

?>

<table border="1">

    <tr>
        <th>Student Name</th>
        <th>Matric Number</th>
        <th>IC Number</th>
        <th>Update</th>
        <th>Delete</th>
    </tr>

    <?php

        if( $result->num_rows > 0 ) {

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

                echo "<tr>";
                echo "<td>" . $row["name"] . "</td>";
                echo "<td>" . $row["matric"] . "</td>";
                echo "<td>" . $row["ic"] . "</td>";
                echo "<td><input type=button value=Update></td>";
                echo "<td><form method='POST'>
                <input type=hidden name=id value=".$row["id"]." >
                <input type=submit value=Delete name=delete >
                </form>
                </td>";
                echo "</tr>";
            }

        } else {  
            die("0 results");
        }  
    ?>

</table>

You can also create delete links(ie test.php?delete_id=100) for each row separately instead of creating form and GET ID to delete on server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM