单击删除链接时删除数据库行

时间:2016-10-14 03:26:01

标签: php hyperlink click html-table delete-row

以下PHP脚本用于检索数据库中的一组数据(订单号)

<?php
            require("includes/db.php");

            $sql="SELECT * FROM `order` ";
            $result=mysqli_query($db,$sql);
echo"<head>";
echo'
        <link rel="stylesheet" href="view.css">
            <head>


        ';
echo"</head>";

echo "<body >";
echo "<table border=1 cellspacing=0 cellpadding=4 > " ;
echo"<tr bgcolor=grey>";
echo"<td align=center>";
echo "<font size=4>";
echo "<B>";
echo "Order No.";
echo "</B>";
echo"</td>";
echo"</tr>";


while($row=mysqli_fetch_array($result))
{

    echo"<tr>";
     echo"<td align=center>";
    echo $row["OrderNo."];
     echo "<br>";
     echo"</td>";
    echo"<td align=center>";
    echo "<a href='delete.php?del=";
    echo $row['OrderNo.'];
    echo">delete</a>";
     echo "<br>";
     echo"</td>";
    echo"</tr>";
}
    echo"</table>";

?>

单击每个ow中的删除链接时,必须删除该特定行!删除的PHP脚本如下

< ?php
     include("includes/db.php");

    if( isset($_GET['del']) )
    {
        $id = $_GET['del'];
        $sql= "DELETE FROM order WHERE OrderNo.='$id'";
        $res= mysqli_query($db,$sql) or die("Failed".mysql_error());

    }
?> 

我没有将第二个PHP脚本重定向到初始脚本以识别错误!当点击删除链接时,我在屏幕上得到以下内容,没有删除该行!

< ?php 
    include("includes/db.php"); 
    if( isset($_GET['del']) ) { 
        $id = $_GET['del']; 
        $sql= "DELETE FROM order WHERE OrderNo.='$id'"; 
        $res= mysqli_query($db,$sql) or die("Failed".mysql_error()); 
    } 
?>

如何在单击删除链接时删除一行来纠正此问题?

3 个答案:

答案 0 :(得分:1)

试试这个

$sql= "DELETE FROM `order` WHERE `OrderNo.` = '$id' ";

html表行也有问题。在删除之前,您尚未关闭单引号(&#39;)。请尝试使用以下代码。

echo "<td align=center>";
echo "<a href='delete.php?del=";
echo $row['OrderNo.'];
echo "'>delete</a>";
echo "<br>";
echo"</td>";

答案 1 :(得分:0)

试试这个:

$sql= "DELETE FROM order WHERE OrderNo = '".$id."'";

答案 2 :(得分:0)

你的第二个代码说明了一切。 将< ?php更改为<?php。你首先错误地将文件标识为php脚本。