PHP开发新闻管理系统之删除功... 登录

PHP开发新闻管理系统之删除功能的实现

之前我们讲过展示页面,修改和删除都带了一个输出id的语句,代码去下:

<a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a>

<a href="delnew.php?id=<?php echo $row['id'];?>">删除</a>

可以看出,点击删除,跳转到 delnew.php 页面

注意:删除,我们也是要获取id,然后在数据库进行查询,写删除语句,必须要有条件,不然就不知道删除哪条数据了

删除功能的流程图:

del.png

首先连接数据库

    header("Content-type: text/html; charset=utf-8");//设置编码
    $con =@mysql_connect("localhost","root","root") or die("数据库连接失败");
    mysql_select_db('news') or die("指定的数据库不能打开");
    mysql_query("set names utf8");//设置数据库的字符集

然后获取 id

$id = $_GET['id'];

最后我们写删除语句

    $sql = "delete from new where id='$id'";
    $res = mysql_query($sql);
    if($res){
        echo "<script>alert('删除成功');location.href='newlist.php';</script>";
    }else{
        echo "<script>alert('删除失败');location.href='newlist.php';</script>";
    }

这样我们的删除功能就已经实现了


完整代码如下

<?php
    header("Content-type: text/html; charset=utf-8");//设置编码
    $con =@mysql_connect("localhost","root","root") or die("数据库连接失败");
    mysql_select_db('news') or die("指定的数据库不能打开");
    mysql_query("set names utf8");//设置数据库的字符集

    $id = $_GET['id'];
    
    $sql = "delete from new where id='$id'";
    $res = mysql_query($sql);
    if($res){
        echo "<script>alert('删除成功');location.href='newlist.php';</script>";
    }else{
        echo "<script>alert('删除失败');location.href='newlist.php';</script>";
    }
?>
下一节
<?php header("Content-type: text/html; charset=utf-8");//设置编码 $con =@mysql_connect("localhost","root","root") or die("数据库连接失败"); mysql_select_db('news') or die("指定的数据库不能打开"); mysql_query("set names utf8");//设置数据库的字符集 $id = $_GET['id']; $sql = "delete from new where id='$id'"; $res = mysql_query($sql); if($res){ echo "<script>alert('删除成功');location.href='newlist.php';</script>"; }else{ echo "<script>alert('删除失败');location.href='newlist.php';</script>"; } ?>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送