【PHP CMS系统】 新闻管理系统 2
小项目代码:
manager.html
<html> <head> <meta http-equiv="content-type" content="text/html; charset="utf-8" /> </head> <h1>Manager</h1> <hr/> <a href="addNews.html">Add news</a> <a href="newList.php">Update</a> <a href="">Show all news</a> </html>
addNews.html
<head> <title>新闻标题</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> </head> <!--我们在添加新闻时,就同时生成一个对应的新闻页面(比如你设计好的一个新闻内容显示模板)--> <form action="newsController.php" method="post"> <table> <tr><td>新闻标题</td><td><input type="text" name="title"/></td></tr> <tr><td>新闻内容</td><td><textarea cols="50" rows="10" name="content"></textarea></td></tr> <tr><td><input type="submit" value="添加"/></td><td><input type="reset" value="重新填写"/></td></tr> <!--隐藏区--> <input type='hidden' name='oper' value='add'/> </table> </form> </html>
newsController.php
<?php //处理用户的dml请求 //先获取oper值 $oper = $_POST['oper']; if($oper === "add"){ $title = $_POST['title']; $content = $_POST['content']; $con = mysql_connect("localhost","root","root"); //添加新闻到数据库 if(!$con){ die('Could not connect'. mysql_error()); } mysql_select_db("news",$con); $sql = "insert into news values(null, '$title', '$content', '')"; if(mysql_query($sql,$con)){ //creat static page $id = mysql_insert_id(); $html_filename = 'new-id'.$id.'.html'; //取出当前的年月日创建一个文件夹,把这个静态页面放入这个文件夹中 //safe_mode ,如果打开这个模式,那么创建文件夹的功能就失效了 $html_fp = fopen('../'.$html_filename, 'w'); //把模板文件读取进来 $fp = fopen('news.tpl', 'r'); //循环读取 while(!feof($fp)){ $row = fgets($fp); //把占位符替换掉-->myReplace $row = str_replace('%title%',$title,$row); //在row中找有没有%title%的字符串,找到后用$title替换 $row = str_replace('%content%',$content,$row); //在row中找有没有%title%的字符串,找到后用$title替换 fwrite($html_fp, $row); } //close file fclose($fp); fclose($html_fp); echo "恭喜你,添加成功<a href='manager.html'>管理新闻</a>"; //如果希望添加完新闻就立即更新 include 'newList.php'; }else{ die("Insert faild"); } }else if($oper === "update"){ }else if($oper === "delete"){ } ?>
newList.php
<?php $con = mysql_connect("localhost","root","root"); if(!$con){ die('Could not connect'. mysql_error()); } mysql_select_db("news",$con); $sql = "select * from news"; $res = mysql_query($sql); ob_start(); echo "<head><meta http-equiv='content-type' content='text/html;charset=utf-8' /></head>"; echo "<h1>新闻列表</h1>"; echo "<table>"; echo "<tr><td>id</td><td>标题</td><td>查看详情</td><td>修改新闻</td></tr>"; while($row=mysql_fetch_assoc($res)){ echo '<tr><td>'.$row['id'].'</td><td>'.$row['title'].'</td><td><a href="new-id'.$row['id'].'.html">See More</a></td></tr>'; } echo "</table>"; //这里我们可以把ob内容取出,并生成一个静态页面 $ob_str = ob_get_contents(); file_put_contents('../index.html',$ob_str); ob_clean(); echo "Success, <a href='../index.html'>Click to check</a>"; mysql_free_result($res); ?>
new.tpl
<html> <head> <meta http-equiv='content-type' content="text/html; charset=utf-8" /> <title>%title%</title> </head> <body> <h1>%title%</h1> <hr/> <pre>%content%</pre> </body> </html>
http://localhost/bigwebsite/staticPage/newPage/manager/manager.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具