PHP开发简单新闻发布系统之新... 登录

PHP开发简单新闻发布系统之新闻修改页功能实现

上一节讲解了PHP开发简单新闻发布系统之新闻修改页面和从新闻列表页点击“修改”

后直接跳转到新闻修改页面并显示出内容。

本节讲解如何通过PHP代码实现新闻修改页的编辑修改功能。

1609.png

首先还是要连接数据库 test 和表 new:

<?php
$link = mysqli_connect('localhost','uesename','password','test');
    mysqli_set_charset($link, "utf8");
if (!$link) {
  die("连接失败:".mysqli_connect_error());
}
?>

用POST方式获取值,这里我们需要更新三个项目:标题 title, 作者 author, 新闻内容content

<?php
$id = isset($_POST['id'])?$_POST['id']:"";      //获取id的值

$title = isset($_POST['title'])?$_POST['title']:"";

$author = isset($_POST['author'])?$_POST['author']:"";

$content = isset($_POST['content'])?$_POST['content']:"";
?>

使用SQL语句中的 update:更新数据

<?php
$sql="update new set title = '$title',author = '$author',content = '$content' where id = '$id'";
//echo $sql;
$rel=mysqli_query($link,$sql);//执行sql语句
//echo $rel
?>

这样我们就可以实现完整的修改功能

完整的update.php代码:

<?php
  header("content-type:text/html;charset=utf-8");
  $link = mysqli_connect('localhost','username','password','test');
      mysqli_set_charset($link, "utf8");
  if (!$link) {
    die("连接失败:".mysqli_connect_error());
  }
  
  $id = isset($_POST['id'])?$_POST['id']:"";
    $title = isset($_POST['title'])?$_POST['title']:"";
    $author = isset($_POST['author'])?$_POST['author']:"";
    $content = isset($_POST['content'])?$_POST['content']:"";
    
    $sql="update new set title = '$title',author = '$author',content = '$content' where id = '$id'";
    //echo $sql;
    $rel=mysqli_query($link,$sql);//执行sql语句
    //echo $rel
  
  if($rel){
    echo "<script>alert('新闻修改成功');window.location.href='list.php'</script>";
  }else{
    echo "<script>alert('新闻修改失败');window.location.href='edit.php'</script>";
  }
?>


至此我们的PHP开发之简单新闻发布系统就全部介绍完成了,朋友们可以通过学习把本章的代码页联合起来使用,

实现完整的简单新闻发布系统的增删改查,分页,搜索功能。

注:本章课程只是简单演示,其代码仅供学习参考,不可直接用于项目。

<?php header("content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } $id = isset($_POST['id'])?$_POST['id']:""; $title = isset($_POST['title'])?$_POST['title']:""; $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; $sql="update new set title = '$title',author = '$author',content = '$content' where id = '$id'"; //echo $sql; $rel=mysqli_query($link,$sql);//执行sql语句 //echo $rel if($rel){ echo "<script>alert('新闻修改成功');window.location.href='list.php'</script>"; }else{ echo "<script>alert('新闻修改失败');window.location.href='edit.php'</script>"; } ?>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送