0


基于PHP和MySQL的新闻发布系统

关于世界杯⚽️

国际足联世界杯(FIFA World Cup),简称“世界杯”,是由全世界国家级别球队参与,象征足球界最高荣誉,并具有最大知名度和影响力的足球赛事。世界杯全球电视转播观众超过35亿 。世界杯每四年举办一次,任何国际足联会员国(地区)都可以派出代表队报名参加这项赛事。

今年在卡塔尔举行的世界杯也是赚足了广大球迷的眼球,奈何自己不是很懂,只能看个热闹,恰好最近学习了

PHP连接数据库

,简单完成一个关于“世界杯”的

新闻发布系统

练练手,也算是对世界杯有个小回忆了。

展示效果

展示页面:

在这里插入图片描述在这里插入图片描述

新闻发布后台:
在这里插入图片描述

整理思路

我们可以做一个思维导图来整理下自己的思路,也方便能够清晰地写出各个功能页面:在这里插入图片描述

首先是一个用于展示已发布新闻的页面,那么如何让这些新闻展示出来呢——那就需要用到SQL语句中的

查询语句


然后就是管理员:通过注册的方式获取对新闻的操作权限,注册完成后再登录进入新闻发布的后台,对新闻进行

发布

删除

修改

操作。
🆗,这就是大体思路。

功能实现

首先做好准备工作:
开启数据库和服务器
在这里插入图片描述

通过数据库管理工具创建相关数据表
新闻表:

在这里插入图片描述

用户表:
在这里插入图片描述

测试数据库连接情况

//测试文件conn.php,测试数据库连接情况<?php
    header("content-type:text/html;charset=utf-8");$mysqli=newmysqli("localhost:3306","root","","2021info");if(!$mysqli){die("error");}$mysqli->query(("set names utf-8"));?>//连接成功

展示页面

<?php// 该页面用于展示新闻,并提供登录和注册入口进入后台对新闻进行操作include"./conn.php";//能够实现所需功能的SQL语句$sel="select * from news order by mtime limit 0,5";//执行SQL语句$rs=$mysqli->query($sel);//将查询得到的语句转化成一维数组$result=$rs->fetch_assoc();//用来设置服务器的默认时区,Asia表示亚洲,Shanghai用来表示中国上海date_default_timezone_set("Asia/Shanghai");?><!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>新闻展示页面</title><!--<link rel="stylesheet" type="text/css" href="./css/导航条.css"/>--><link rel="stylesheet" type="text/css" href="./css/index1.css"/><link rel="stylesheet" type="text/css" href="./css/lunbo.css"/></head><body style="background-color:rgb(60, 108, 236);color: antiquewhite;"><div class="bd"><img src="./img/世界杯.png" alt=""></div><div class="div1"><p>点击观看精彩赛事</p><div class="demo"><a href="#"><div class="demo1"><img src="./img/pic1.png" alt=""><img src="./img/pic2.png" alt=""><img src="./img/pic3.png" alt=""><img src="./img/pic4.png" alt=""><img src="./img/pic5.png" alt=""></div></a></div><div class="div2"><img src="./img/世界杯1.png"><h4 class="font1">新闻</h4><h4 class="font2">发布时间</h4>//利用列表将需要展示的新闻字段展示出来<?php while($result=$rs->fetch_assoc()):?><li class="title"><a href="watch.php?id=<?= $result['id'] ?>"><span class="title"><a href="contents.php?id=<?= $result['id'] ?>"><?php
                                //利用mb_strlen()函数和mb_substr()函数,返回新闻标题的一部分,多出部分用“...”代替,使之更加美观if(mb_strlen($result['title'])>3){echomb_substr($result['title'],0,6)."...";}else{echo$result['title'];}?></span></li>//与 date_default_timezone_set("Asia/Shanghai");共同将时间戳转换成日期<li class="ctime"><?=date("Y-m-d H:i:s",$result['mtime'])?></li></a><?php endwhile;?></div><div class="div3"><a href="./regis.php">注册</a><a href="./login.php">登录</a></div></div></body></html>
//watch.php<?php
include"./conn.php";date_default_timezone_set("Asia/Shanghai");$id=$_GET['id'];$sel="select title,author,content,mtime from news where id={$id};";$re=$mysqli->query($sel);$result=$re->fetch_assoc();// var_dump($result);// if(!$re){//     echo "<script>alert('失败');location.href='newsshow.php';</script>";// }?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" type="text/css" href="./css/watch.css"/></head><body style="background-color:rgb(60, 108, 236)"><div class="div1"><div class="titleBox"><h2><span><?=$result['title']?></span></h2></div><div class="author"><h4>发布者:<span><?=$result['author']?></span></h4></div><div class="content"><p><?=$result['content']?></p></div><div class="mtime"><p><?=date("Y-m-d H:i:s",$result['mtime'])?><a href="./newshow.php">返回首页</a></p></div></div></body></html>

注册登录

注册:
在这里插入图片描述

//register.html
<h3>sign up</h3><formaction="register_do.php"method="post"id="myform">
        请输入用户名:<inputtype="text"name="username"id="username">
        请输入手机号:<inputtype="text"name="usertel"id="usertel">
        请输入邮箱:<inputtype="text"name="useremail"id="useremail">
        请输入密码:<inputtype="password"name="userpwd"id="userpwd">
        请确认密码:<inputtype="password"name="repwd"id="repwd"><inputtype="submit"value="注册"></form><scriptsrc="./js/register.js"></script>
//js实现用户输入信息是否正确//设置页面加载完成即执行js代码
window.onload=function(){//获取id对象var myform=document.getElementById("myform");var username=document.getElementById("username");var usertel=document.getElementById("usertel");var useremail=document.getElementById("useremail");var pwd=document.getElementById("userpwd");var repwd=document.getElementById("repwd");
    myform.onsubmit=function(){if(username.value==""){alert("用户名不可为空!");returnfalse;}if(usertel.value.length!=11){alert("请输入正确手机号!");returnfalse;}if(useremail.value.indexOf('@')<1|| useremail.value.indexOf('.')<3){alert("请输入正确邮箱!");returnfalse;}if(userpwd.value.length==0){alert("密码不能为空!");returnfalse;}if(userpwd.value.length<6){alert("密码不可少于6位!");returnfalse;}if(repwd.value==""){alert("确认密码不可为空!");returnfalse;}if(repwd.value!=pwd.value){alert("密码输入不一致!");returnfalse;}}}
<?php//register_do.php// 该页面任务是拿到resigter的页面传递的数据,并进行用户名重复性检查.include"./conn.php";//post传递参数$username=$_POST['username'];$pwd=md5($_POST['pwd']);$repwd=md5($_POST['repwd']);$sel="select * from userpub where username='{$username}';";$rs=$mysqli->query($sel);//从查询中输出所影响记录行数,利用影响的行数来判断是否重复注册用户名$rows=$mysqli->affected_rows;//若影响的行数大于0,则代表重复注册用户名if($rows>0){echo"<script>alert('用户名注册失败,请重新选择用户名');location.href='regis.php';</script>";}else{//没有影响行数代表用户名可注册,执行插入语句$in="insert into userpub(username,password,repwd)values('{$username}','{$pwd}','{$repwd}');";$st=$mysqli->query($in);if($st){echo"<script>alert('注册成功');location.href='login.php';</script>";}else{echo"<script>alert('注册失败');location.href='register.php';</script>";}}?>

登录:

在这里插入图片描述

//login.js
window.onload=function(){//获取id 对象var myform=document.getElementById("myform");var user=document.getElementById("username");var pwd=document.getElementById("userpwd");
            myform.onsubmit=function(){if(username.value==""){alert("用户名不可为空!");returnfalse;}if(userpwd.value.length==0){alert("密码不能为空!");returnfalse;}}}
//login_do.php<?php
// 该页面将登录页面收集到的信息与后台注册的用户名和密码进行核对,核对成功进入数据库include"./conn.php";$username=$_POST['username'];$userpwd=md5($_POST['userpwd']);$sel="select * from admin where username='{$username}';";$rs=$mysqli->query($sel);//从查询中输出所影响记录行数,利用影响的行数来判断是否重复注册用户名$rows=$mysqli->affected_rows;if($rows>0){// 用户名存在  验证用户输入的密码和数据表中存在的用户名对应的密码是否一致,将用户名对应的字段转换成一维数组,通过数组名加下标的方式判断用户密码输入是否正确。$result=$rs->fetch_assoc();if($userpwd==$result['userpwd']){echo"<script>alert('登录成功');location.href='news_select.php'</script>";}else{echo"<script>alert('密码错误,请重新输入或注册');location.href='login.php'</script>";}}else{// 用户名不存在echo"<script>alert('用户名不存在,请重新输入或者先注册');location.href='login.php';</script>";}?>

增加——insert

在这里插入图片描述

// news_insert.html 该页面用于收集新闻相关信息<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><form action="insert_do.php" method="post">
    请输入新闻标题:<input type="text" name="title"></br>
    请输入新闻内容:<textarea name="content" id="" cols="30" rows="10"></textarea></br>
    请输入新闻作者:<input type="text" name="author"><input type="submit" value="发布"></form></body></html>
//insert_do.php 该页面拿到相关的新闻信息,并执行插入SQL语句将其插入到news表中<?php
    include'./conn.php';$title=$_POST['title'];$content=$_POST['content'];$author=$_POST['author'];$ctime=time();$mtime=time();$in="insert into news(title,content,author,ctime,mtime)values('{$title}','{$content}','{$author}',$ctime,$mtime);";$st=$mysqli->query($in);if($st){echo"<script>alert('发布成功!');location.href='news_select.php';</script>";}else{echo"<script>alert('发布失败!');location.href='insert_do.php';</script>";}?>

查询——select

在这里插入图片描述

//news_select.php<?php
// 该页面用于展示从数据库中最新的几条新闻include"./conn.php";$sel="select * from news order by ctime desc limit 0,10;";$rs=$mysqli->query($sel);date_default_timezone_set("Asia/Shanghai");?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><table border="1px"><tr><th>id</th><th>title</th><th>content</th><th>author</th><th>发布时间</th><th>修改时间</th><th>修改</th><th>删除</th></tr><?php
        while($result=$rs->fetch_assoc()){?><tr><td><?=$result['id']?></td><td><?=$result['title']?></td><td><?=$result['content']?></td><td><?=$result['author']?></td><td><?=date("Y-m-d H:i:s",$result['ctime'])?></td><td><?=date("Y-m-d H:i:s",$result['mtime'])?></td><td><a href="news_update.php?id=<?=$result['id']?>">修改</a></td><td><a href="news_delete.php?id=<?=$result['id']?>">删除</a></td></tr><?php }?></table><a href="./news_insert.html">发布新闻</a><a href="./newshow.php">回到首页</a><a href="./delnews.php">查看已删新闻</a></body></html>

修改——update

//news_update.php 该页面用于收集对新闻的修改信息<?php
include"./conn.php";//get传参 id$id=$_GET['id'];?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><form action="update_do.php?id=<?=$id?>" method="post">
        请输入新的标题:<input type="text" name="newtitle"></br>
        请输入新的内容:<textarea name="newcontent" id="" rows="10" cols="30"></textarea></br>
        请输入新闻作者:<input type="text" name="newauthor"><!-- 隐藏域获取id --><input type="hidden" name="id" value="<?=$id?>"><input type="submit" value="修改"></form></body></html>
//update_do.php  该页面拿到收集到的相关修改信息,并执行修改的SQL语句,完成对数据表的修改。<?php
    require("./conn.php");$id=$_POST['id'];$title=$_POST['newtitle'];$content=$_POST['newcontent'];$author=$_POST['newauthor'];$mtime=time();$upd="update  news set title='{$title}',content='{$content}',author='{$author}',mtime=$mtime where id={$id};";$st=$mysqli->query($upd);if($st){echo"<script>alert('修改成功');location.href='news_select.php';</script>";}else{echo"<script>alert('修改失败');location.href='news_select.php';</script>";}?>

删除——delete

<?phpinclude"./conn.php";$id=$_GET['id'];$del="delete from newpub where id={$id};";$st=$mysqli->query($del);if($st){echo"<script>alert('删除成功');location.href='newselect.php';</script>";}else{echo"<script>alert('删除失败');location.href='newselect.php';</script>";}?>

优化功能

在这里插入图片描述

查看已删新闻

数据表:
如果我们想要查看已经删除的新闻的话,就需要再创建一个数据表来存储删除的新闻信息,这里我创建了一个

delnews 表

;并增加了

删除时间 字段

在这里插入图片描述

//news_delete.php 该页面将所要删除的记录放到一个新的表中,并从原来表中删除。<?php
    include"./conn.php";$id=$_GET['id'];$dtime=time();date_default_timezone_set("Asia/Shanghai");// 将获取的id相应的数据读取成一个result数组中,然后利用数组将读取的字段插入到delnews表中。$sel="select * from news where id={$id};";$re=$mysqli->query($sel);$result=$re->fetch_assoc();//  var_dump($result);$in="insert into delnews(id,title,content,author,ctime,mtime,dtime)values('{$result['id']}','{$result['title']}','{$result['content']}','{$result['author']}',
    '{$result['ctime']}','{$result['mtime']}',$dtime);";$st=$mysqli->query($in);if($st){$del="delete from news where id={$id};";$de=$mysqli->query($del);if($de){echo"<script>alert('删除成功!');location.href='news_select.php';</script>";}else{echo"<script>alert('删除失败!');location.href='news_select.php';</script>";}}else{echo"<script>alert('删除失败!');location.href='news_select.php';</script>";}?>
<?php//delnews.php 该页面用于从delnews表中读取内容,以表格的形式展出。include"./conn.php";$sel="select * from delnews order by dtime desc";$re=$mysqli->query($sel);date_default_timezone_set("Asia/Shanghai");?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><table border="1px"><tr><th>title</th><th>content</th><th>author</th><th>发布时间</th><th>修改时间</th><th>删除时间</th><th>撤销删除</th></tr><?php while($result=$re->fetch_assoc()){?><tr><td><?=$result['id']?></td><td><?=$result['title']?></td><td><?=$result['author']?></td><td><?=date("Y-m-d H:i:s",$result['ctime'])?></td><td><?=date("Y-m-d H:i:s",$result['mtime'])?></td><td><?=date("Y-m-d H:i:s",$result['dtime'])?></td><td><a href="delnews_do.php?id=<?= $result['id'] ?>">撤销删除</a></td></tr><?php }?></table></body></html>

撤销已删新闻

撤销功能和对news表中数据的删除功能类似:

//delnews_do.php 该页面用于撤销删除已删新闻。<?php
include"./conn.php";$id=$_GET['id'];$mtime=time();date_default_timezone_set("Asia/Shanghai");$sel="select * from delnews where id={$id};";$rs=$mysqli->query($sel);$result=$rs->fetch_assoc();$in="insert into news(id,title,content,author,ctime,mtime)values('{$result['id']}','{$result['title']}','{$result['content']}','{$result['author']}',
'{$result['ctime']}','{$result['mtime']}');";$st=$mysqli->query($in);if($st){$del="delete from delnews where id={$id};";$de=$mysqli->query($del);if($de){echo"<script>alert('撤销成功!');location.href='news_select.php';</script>";}else{echo"<script>alert('撤销失败!');location.href='news_select.php';</script>";}}else{echo"<script>alert('撤销失败!');location.href='news_select.php';</script>";}

总结🥇

刚开始接触php连接数据库的时候可能会遇到很多小问题,首先就是

检查数据库的连接状态是否正常

,再就是注意

参数的传递

SQL语句的写法

等。
当然还有更多需要优化的地方:没有进行

cookie和session

来记录用户状态,以及增强存储在服务器的数据的安全性;在基础功能上还需要添加更多改善用户体验的方面。那么在后续的文章还会给大家分享学习经验,继续优化这个新闻发布系统。
如有不足,感谢指正!

标签: php mysql 开发语言

本文转载自: https://blog.csdn.net/weixin_64122448/article/details/128269481
版权归原作者 沃和莱特 所有, 如有侵权,请联系我们删除。

“基于PHP和MySQL的新闻发布系统”的评论:

还没有评论