如何在PHP中请求用户ID或删除行

时间:2014-10-15 20:17:04

标签: php mysql

我需要删除php中的特定行..所以如何获取ID或其他方式删除特定记录

dbconnect.php只是一个简单的数据库连接

<?php


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

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

?>

index.php用户可以看到的页面

<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删除脚本

   <?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>";
?>

提前致谢

1 个答案:

答案 0 :(得分:2)

index.php中的

使用:

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

然后在delete.php中使用$_GET['user_id'];