无法获取要删除的行 PHP

Can't get row to delete PHP

这是我的 delete.php 页面:

<?php
//including the database connection file
include("config.php");

//getting id of the data from url
$id = $_GET['id'];

//deleting the row from table
$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

//redirecting to the display page (index.php in our case)
header("Location:index.php");
?>

调用命中的 index.php 页面有:

<?php
//including the database connection file
include("config.php");

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id ASC"); 
// using mysqli_query instead


while($res = mysqli_fetch_array($result))
    {
?>
<table>
<tr>
    <td class="long"><?php echo $res['nameFirst'];?></td>
    <td class="long"><?php echo $res['nameLast'];?></td>
    <td class="long"><?php echo $res['email'];?></td>
    <td><a href="edit.php?id=$res[id]">Edit</a></td>
    <td><a href="delete.php?id=$res[id]" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>
</tr>
<?php } ?>

</table>

但是当我单击删除 link 时,页面要求确认,然后在重新加载 index.php(本身)时刷新自身,但 table 中的行没有删除。

我确信这是一个简单的修复,但看不到问题所在。我假设错误是在 $result 行的某处?

提前致谢。

您在inxde.php中传递id参数时没有显示ID。请参阅以下正确代码:

<?php
//including the database connection file
include("config.php");

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id ASC"); 
// using mysqli_query instead


while($res = mysqli_fetch_array($result))
    {
?>
<table>
<tr>
    <td class="long"><?php echo $res['nameFirst'];?></td>
    <td class="long"><?php echo $res['nameLast'];?></td>
    <td class="long"><?php echo $res['email'];?></td>
    <td><a href="edit.php?id=$res[id]">Edit</a></td>
    <td><a href="delete.php?id=<?php echo $res['id']; ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>
</tr>
<?php } ?>

</table>

修复了这些行:

<td><a href="edit.php?id=$res['id']">Edit</a></td>
<td><a href="delete.php?id=$res['id']" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>

$res[id] 需要是 $res['id'],因为您将列名称作为字符串传递。

你忘了回显

<a href="delete.php?id=<?php echo $res[id] ?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a>

试试这个

<td><a href="edit.php?id="<?php echo $res['id']; ?>>Edit</a></td>
<td><a href="delete.php?id="<?php echo $res[id]; ?> onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>

并在 delete.php

$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=".$id);

这是因为,你弄乱了 HTML 和 PHP 的代码。

并且 PHP 代码未生成 ID。

我们可以在 PHP 文件的任何地方交替编写 PHP、HTML 和 Javascript 代码。

但是,你需要说明哪个是PHP码,哪个是HTML码(如果没有指定默认一个)哪个是Javascript码。

修改行:

<td><a href="delete.php?id=$res[id]" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>

收件人:

<td><a href="delete.php?id=<?php echo $res['id'];?>" onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>