我有以下代码在表中加载用户:register.php
这里我要删除用户。只有第二个脚本deleteUsers.php
不起作用。
任何人都可以帮助我吗?
register.php`
<?php
$edit = 'edit:';
$account = 'Username:';
$account = '<font size="4">'.$account.'</font>';
$password1 = 'Password:';
$password1 = '<font size="4">'.$password1.'</font>';
$id = 'id:';
$id = '<font size="4">'.$id.'</font>';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table border=4><tr><td>$account</td><td>$password1</td><td>$id</td><td>edit</td><td>delete</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php'> delete</a> </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>
您会看到有一个重定向到deleteUser.php
的删除按钮
这是您在下面找到的页面,遗憾的是删除功能无效:
<?php
require_once("/db_conn.php");
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
(isset($_GET['id'])
// delete the entry
$result = mysql_query("DELETE FROM login WHERE id =$id")
or die(mysql_error());
// redirect back to the view page
header("Location: register.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
echo "doesn'twork";
}
?>
答案 0 :(得分:1)
您需要传递id变量:
while($row = $result->fetch_assoc())
{
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php?id=" . $row['id'] . "'> delete</a> </td></tr>";
}
答案 1 :(得分:0)
您需要传递id
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php?id=". $row['id']."'> delete</a> </td></tr>";
}
on deleteuser.php
$id=$_GET['id'];
$result = mysql_query("DELETE FROM login WHERE id =$id")
or die(mysql_error());
答案 2 :(得分:-1)
$id=$_GET['id'];
必须在查询后放置