dongpa6867 2017-04-19 05:25
浏览 10
已采纳

无法获取行删除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.

  • 写回答

5条回答 默认 最新

  • dongzuo9096 2017-04-19 05:34
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源