带警报的删除按钮在php中不起作用

时间:2016-08-24 16:04:55

标签: php mysql sql-delete delete-row

如果我点击删除按钮,然后请求我的许可删除,但点击确定后不删除。从我的代码 id for delete not get。在我的浏览器搜索框中显示:

本地主机/新/树1 / 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'");
   }

2 个答案:

答案 0 :(得分:1)

你在这里没有回应你的变量。

                                   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?

您当前的代码容易受到攻击。

答案 1 :(得分:0)

您忘了将$ row [&#39; id&#39;]的回显更改为此行:

fulfill()

有关

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

你的sql for delete这行是坏的,同样,你有一个sintaxis错误,缺少 FROM 这个词,更新这一行:

<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=mysql_query("DELETE descriptionoftree WHERE id='$cid'");

并测试它

此致