无法获取行删除PHP

Here is my delete.php page:

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

and the index.php page that is calling hit has:

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

But when I click the Delete link, the page asks for confirmation, and then refreshes itself as it reloads index.php (itself) but the row in the table does not delete.

I am sure this is a simple fix but can't see the problem. I am presuming the error is somewhere in the $result line?

Thank you in advance.

Its because, you are messing up with the code of HTML and PHP.

And PHP code is not generating the id.

We can write PHP, HTML and Javascript code interchangeably in PHP file at any place.

But, you need to mention which is PHP code, which is HTML code (default one if nothing is specified) and which is Javascript code.

Modify the line:

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

To:

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

You haven't show the ID when you pass id parameter in the inxde.php. see the below correct code:

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

Fixed these lines:

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

The $res[id] needs to be $res['id'], because you are passing the column name as string.

You forgot to echo

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

Try this

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

and in delete.php

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