简体   繁体   中英

How to request user ID or delete a row in PHP

I need to delete a specific row in php.. so how could I get the ID or other way to delete a specific record

dbconnect.php just a simple database connection

<?php


    $con = mysqli_connect("localhost","root","","phpractice");

    if(mysqli_connect_errno()){
        echo "Database connection failed";
    }

?>

index.php the page where the user can see

<html>
<head>
<link rel="stylesheet" type="text/css" href="../styles/index.css">
</head>

<title>Home Page</title>

<?php include'../script/dbconnect.php';?>

<body>
<div id="container">

<div id="table">

<?php

 $result = mysqli_query($con,"SELECT * FROM users");


echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Password</th>
<th colspan=2>Controls</th>
</tr>
";

 while($row = mysqli_fetch_array($result)){
    echo "<tr>";
    echo "<td>".$row['firstname']."</td>";
    echo "<td>".$row['lastname']."</td>";
    echo "<td>".$row['username']."</td>";
    echo "<td>".$row['password']."</td>";
    echo "<td>"."<a href='#'>EDIT</a>"."</td>";
    echo "<td><a href='../script/delete.php?id=".$row['user_id']."'>DELETE</a></td>";
    echo "</tr>";

 }

    echo "</table>";
?>
<a href="adduser.php" class="button">Add User</a>

</div><!--table-->
</div><!--container-->


</body>
</html>

delete.php the delete script

   <?php

include '../script/dbconnect.php';

$id = $_GET['user_id'];

$query = "DELETE FROM users WHERE user_id = $id";

mysqli_query($con, $query) or die (mysqli_error($con));

echo "DELETE USER SUCCESSFUL!";
echo "</br>";
echo "<a href='../main/index.php'>RETURN TO DISPLAY</a>";
?>

thanks in advance

in index.php use:

 echo "<td><a href='../script/delete.php?user_id=".$row['user_id']."'>DELETE</a></td>";

then use $_GET['user_id']; in delete.php

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