带提醒的删除按钮在 php 中不起作用

Delete button with alert does not work in php

如果我点击删除按钮,它会请求我删除内容,但点击后,OK内容并没有被删除。

据我了解,我的代码无法捕获 id 来删除内容。

在我的浏览器搜索框中显示:

localhost/new/tree1/view_tree.php?id=

获取id代码如下:

<?php
    include('connect.php');
    $result = mysql_query("SELECT * FROM descriptionoftree WHERE vis=0 order by scientificname");
    while($row = mysql_fetch_array($result))
        {
            echo '<tr>';
            echo '<td >'.$row['scientificname'].'</td>';
            echo '<td>'.$row['english_name'].'</div></td>';
            echo '<td>'.$row['banglaname'].'</div></td>';
            echo '<td>'.$row['groupname'].'</div></td>';
            echo '<td><a href="update_tree.php? id='.$row['id'].'" title="Click To Update"><strong>Update</strong></a></td>';
 ?>
    <td><a href="view_tree.php?id=<?php $row['id']?>
     "onclick="return confirm('Are you sure you want to delete?')"<strong>Delete </strong></a></td>
    <?php
        echo '</tr>';
        }   
        ?>

删除行(内容)代码如下:

if(isset($_GET['id']))
   {
     $cid=$_GET['id'];
     $sql=mysql_query("DELETE descriptionoftree WHERE id='$cid'");
   }

您没有在此处回应您的变量。

                                   vvv
<td><a href="view_tree.php?id=<?php $row['id']?>

添加回显

<td><a href="view_tree.php?id=<?php echo $row['id']?>

或使用短回显标签

<td><a href="view_tree.php?id=<?= $row['id'] ?>

另外,您的删除查询是错误的。你错过了 FROM.

应该是这样的:

$sql=mysql_query("DELETE FROM descriptionoftree WHERE id='$cid'");

也尝试阅读 SQL 注入,How can I prevent SQL injection in PHP?

您当前的代码存在漏洞。

您忘记使您的 $row['id'] 的 echo 更改此行:

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

对于

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

而你的 sql 删除行是错误的,同样,你有一个语法错误,缺少单词 FROM,更新此行:

$sql=mysql_query("DELETE descriptionoftree WHERE id='$cid'");

对于

$sql=mysql_query("DELETE FROM descriptionoftree WHERE id='$cid'");

并进行测试

此致