本文共 25224 字,大约阅读时间需要 84 分钟。
最近因为工作原因需要使用php开发网页,所以开始学习php,在学习的过程中也遇到了很多困难,经过不断的查询百度各种学习资料,逐步的客服了这些困难和疑惑,现在我将学习过程中编写的代码分享给有需要的朋友,仅供参考。
此系统比较简单,共有20个php文件,3个文件夹(css,images,data),php文件分别为conn.php(数据库连接),index.php(首页),type.php(新闻分类页面),newscontent.php(最新新闻页面),admin_login.php(后台登录),chkadmin.php(登录判断),admin.php(后台中心),news_add.php(添加新闻),saveadd.php(添加新闻后台),news_upd.php(修改新闻),saveupdate.php(修改新闻后台),news_del.php(删除新闻),savedel.php(删除新闻后台),type_add.php(添加新闻分类),type_add_save.php(添加新闻分类后台),type_upd.php(修改新闻分类),type_upd_save.php(修改新闻分类后台),type_del.php(删除新闻分类),type_del_save.php(删除新闻分类后台),loginout(注销用户);css样式表2个style.css(前台页面样式),backstage.css(后台页面样式);images文件夹中一个banner文件,data中含有数据库导出文件。此项目源代码资源我已上传至(),请自行查找下载。
1.conn.php(数据库连接)
";}?>
2.index.php(首页)
新闻管理系统 最新新闻: 主 题 | 加入时间 | 详细内容 |
"; echo " $news_title | "; echo " $news_date | "; echo " 查看 | "; echo "
"; } } ?>
"; echo " 共有数据 $total 条,每页显示 $page_size 条; 第 $page 页/共 $page_connt 页: "; if($page >= 2) { echo " 首 页 /前一页 "; } if($page_connt >= 2) { for($i=1; $i<=$page_connt; $i++) { echo " $i "; } } if($page >= 2) { echo " 后一页/尾页"; } echo " | "; echo "
"; ?>
3.type.php(新闻分类页面)
新闻分类 分类新闻: 主 题 | 加入时间 | 详细内容 |
"; echo " [分类:$type_id] $news_title | "; echo " $news_date | "; echo " 查看 | "; echo "
"; } } ?>
"; echo " 共有数据 $total 条,每页显示 $page_size 条; 第 $page 页/共 $page_connt 页: "; if($page >= 2) { echo " 首 页 /前一页 "; } if($page_connt >= 2) { for($i=1; $i<=$page_connt; $i++) { echo " $i "; } } if($page >= 2) { echo " 后一页/尾页"; } echo " | "; echo "
"; ?>
4.newscontent.php(最新新闻页面)
新闻内容
5.admin_login.php(后台登录)
后台登录
6.chkadmin.php(登录判断)
name = $n; $this->password = $p; } function checkinput() { include('conn.php'); $sql = mysqli_query($conn,"SELECT * FROM admin WHERE username='".$this->name."'"); $info = mysqli_fetch_array($sql); if($info == false) { echo ""; exit; } else { if($info['password']==$this->password) { session_start(); $_SESSION['user_name'] = $info['username']; $_SESSION['id'] = $info['id']; header("location:admin.php"); exit; } else { echo ""; exit; } } }}$obj = new chkinput($user_name,$pwd);$obj->checkinput();
7.admin.php(后台中心)
系统后台 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; } ?>
新闻管理系统后台中心: |
添加新闻 |
添加新闻分类 |
分类/管理 |
"; echo " [$type_name] 修改 删除 | "; echo "
"; } ?>
新闻列表: 标 题 | 加入时间 | 详细内容 |
"; echo " $news_title | "; echo " $news_date | "; echo " 修改 删除 | "; echo "
"; } } ?>
"; echo " 共有数据 $total 条,每页显示 $page_size 条; 第 $page 页/共 $page_connt 页: "; if($page >= 2) { echo " 首 页 /前一页 "; } if($page_connt >= 2) { for($i=1; $i<=$page_connt; $i++) { echo " $i "; } } if($page >= 2) { echo " 后一页/尾页"; } echo " | "; echo "
"; ?>
8.news_add.php(添加新闻)
添加新闻 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; echo "
后台首页 "; } ?>
9.saveadd.php(添加新闻后台)
alert('添加成功!');history.go(-1);";?>
10.news_upd.php(修改新闻)
修改新闻 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; echo "
后台首页 "; } ?>
11.saveupdate.php(修改新闻后台)
alert('修改成功!');history.go(-2);";?>
12.news_del.php(删除新闻)
删除新闻 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; echo "
后台首页 "; } ?>
13.savedel.php(删除新闻后台)
alert('删除成功!');history.go(-2);";?>
14.type_add.php(添加新闻分类)
添加分类 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; echo "
后台首页 "; } ?>
15.type_add_save.php(添加新闻分类后台)
alert('添加成功!');history.back();";?>
16.type_upd.php(修改新闻分类)
修改分类 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; echo "
后台首页 "; } ?>
17.type_upd_save.php(修改新闻分类后台)
alert('修改成功!');history.go(-2);";?>
18.type_del.php(删除新闻分类)
删除分类 alert('您还没有登录,请先登录!');history.back();"; exit; } else { $user_name = $_SESSION['user_name']; echo "
管理员:[$user_name] 欢迎您! 注销用户"; echo "
后台首页 "; } ?>
19.type_del_save.php(删除新闻分类后台)
alert('删除成功!');history.go(-2);";?>
20.loginout(注销用户)
21.style.css(前台页面样式),
html *,div{ margin: 0; padding: 0;}body{ font-size: 14px; background-color: #eeeeee;}/*----首页设置----------------------------------------------------------------------*/.container{ width: 1060px; height: 100%; text-align: center; margin: 5px auto;}.container .top_navigation{ width: 100%; height: 50px; background-color: #e6a721; border-radius: 15px 0 0 0;}.container .top_navigation span{ float: left; display: inline-block; width: 300px; height: 48px; font-size: 30px; color: #333333; font-family: "楷体",Arial,"Arial Black"; line-height: 48px; letter-spacing: 3px; text-shadow: 3px 3px 1px #999999; margin-right: 30px;}.container .top_navigation ul{ float: left; width: 730px; list-style: none;}.container .top_navigation ul li{ display: inline; float: left; height: 48px; line-height: 48px; width: 25%; font-size: 22px;}.container .top_navigation li a{ display: inline-block; width: 100%; height: 100%; text-decoration: none; background-color: #e6a721; color: #333333; line-height: 48px;}.container .top_navigation li a:hover{ background-color: #e6c050; color: #ffffff;}.container .top_banner{ width: 100%; float: left; height:400px; overflow: hidden; margin-bottom: 15px; box-shadow: 3px 5px 8px #2d2d2d;}.container .top_banner img{ width: 100%; height: 100%; border: 1px solid #8294bd;}/*------首页内容--------------------------------------------------------------------------*/.container .content{ width: 100%; height: 460px; float: left;}.container .content_left{ width: 260px; height: auto; float: left;}.container .content_left .news_type{ width: 100%; height: auto; text-align: left; padding-top: 10px; padding-left: 30px; font-size: medium;}.container .news_type p{ font-style: oblique; font-weight: bold; color: #2d2d2d;}.container .news_type span{ display: block; margin-left: 20px; margin-bottom: 8px; margin-top: 5px;}.container .news_type span a{ text-decoration: none; color: #2d2d2d;}/*---------首页右侧----------------------------------------------------------------------------*/.container .content_right{ width: 780px; height: auto; float: right;}.container .content_right span{ font-size: 16px; margin-bottom: 10px;}.container .content_right span input[type=text]{ width: 200px; height: 28px; line-height: 28px;}.container .content_right span input[type=submit]{ width: 80px; height: 28px; line-height: 28px; border: 1px solid #237a47;}/*---------首页新闻列表-------------------------------------------------------------------------*/.container .news_list1{ width: 700px; height: auto; color: #333333; margin: 10px auto; border: 1px solid #2d2d2d; border-collapse: collapse;}.container .news_list1 caption{ padding: 8px 20px; text-align: left; color: #ffffff; background-color: #555555;}.container .news_list1 th,.news_list1 td{ border: 1px solid #2d2d2d; height: 30px;}.container .news_list1 .tb_title{ width: 50%;}.container .news_list1 .tb_date{ width: 30%;}.container .news_list1 .tb_detail{ width: 20%;}.container .news_list1 .tb_l{ text-align: left; text-indent: 10px;}.container .news_list1 .tb_c{ text-align: center;}.container .page_list1{ width: 700px; height: auto; color: #2d2d2d; margin: 3px auto; border-bottom: 1px solid #2d2d2d; padding-bottom: 5px; border-collapse: collapse;}.container .page_list1 tr{ height: 40px;}/*-------首页底部----------------------------------------------------*/.container .footer{ width: 100%; float: left; height: 50px; font-size: 16px; font-family: "微软雅黑"; font-style: oblique; line-height: 50px; color: #ffffff; background-color: #e6a721;}/*------新闻分类-------------------------------------------------------------*/.container .content_type{ width: 100%; height: 100%; float: left;}.container .type_list1{ width: 99%; text-align: center; margin: 0 auto; border-top: 1px solid black; border-bottom: 1px solid black; border-collapse: collapse;}.container .type_list1 caption{ text-align: left; margin-bottom: 15px; font-size: 18px; text-indent: 15px;}.container .type_list1 th,.type_list1 td{ height: 30px;}.container .type_list1 th{ border-bottom: 1px solid black; background-color: #555555;}.container .type_list1 td{ border-bottom: 1px dashed black;}.container .type_list1 .tb_title{ width: 60%;}.container .type_list1 .tb_date{ width: 15%;}.container .type_list1 .tb_detail{ width: 25%;}.container .type_list1 .tb_l{ text-align: left; text-indent: 10px;}.container .type_list1 .tb_c{ text-align: center;}.container .page_list2{ width: 99%; height: auto; color: #2d2d2d; margin: 10px auto; padding-bottom: 5px; border-collapse: collapse;}.container .page_list2 tr{ height: 40px;}/*-------新闻内容---------------------------------------------------------*/.container .news_content{ width: 100%; height: auto; float: left; min-height: 400px; background-color: #ffffff;}.container .news_content_list{ width: 90%; height: auto; text-align: left; color: #444444; margin: 0 auto; border-left: 1px dashed #999999; border-right: 1px dashed #999999; padding: 2px 30px;}.container .news_content_list th{ height: 35px;}.container .news_content_list .tb_title{ width: 50%;}.container .news_content_list .tb_date{ width: 50%; text-align: right;}
22.backstage.css(后台页面样式)
html *,div{ margin: 0; padding: 0;}body{ font-size: 14px; background-color: #eeeeee;}/*------后台登录-----------------------------------------------------------*/.login_bk{ background-color: #cccccc;}.login_area{ width: 400px; height: 160px; border: 1px solid #555555; margin: 50px auto; background-color: #999999;}.login_area table{ width: 100%; height: 100%; border-collapse: collapse; text-align: center; font-size: 16px;}.login_area .tb_l{ width: 35%; text-align: right;}.login_area .tb_r{ width: 75%; text-align: left;}.login_area input[type=text],.login_area input[type=password]{ width: 200px; height: 25px; line-height: 25px;}.login_area input[type=submit],.login_area input[type=reset]{ width: 80px; height: 30px; border: 1px solid green; margin-left: 10px;}/*--------后台管理---------------------------------------------------------------------*/.container_bk{ width: 1060px; height: 100%; text-align: center; margin: 5px auto;}.container_bk .top_navigation{ width: 100%; height:30px; float: left; text-align: right;}.container_bk .top_banner{ width: 100%; float: left; height:100px; margin-bottom: 15px; background-color: #e6a721;}.container_bk .top_banner span{ font-size: 28px; font-weight: bold; line-height: 100px; color: #ffffff; font-style: oblique; letter-spacing: 20px;}/*-----------------------------------------------------------------------------------*/.container_bk .content_bk{ width: 100%; height: 460px; float: left;}.container_bk .content_left{ width: 260px; height: auto; float: left;}.container_bk .content_left .news_type{ width: 100%; height: auto; text-align: left; font-size: medium;}.container_bk .news_type table{ width: 100%; height: auto; text-align: left; border-collapse: collapse;}.container_bk .news_type th,.container_bk .news_type td{ height: 28px; line-height: 28px; text-indent: 5px;}/*---------------------------------------------------------------------*/.container_bk .content_right{ width: 780px; height: auto; float: right;}.container_bk .content_right span{ font-size: 16px; margin-bottom: 10px;}.container_bk .content_right span input[type=text]{ width: 200px; height: 28px; line-height: 28px;}.container_bk .content_right span input[type=submit]{ width: 80px; height: 28px; line-height: 28px; border: 1px solid #237a47;}/*------------------------------------------------------------------------------*/.container_bk .news_list1{ width: 700px; height: auto; color: #333333; margin: 10px auto; border: 1px solid #2d2d2d; border-collapse: collapse;}.container_bk .news_list1 caption{ padding: 8px 20px; text-align: left; color: #ffffff; background-color: #555555;}.container_bk .news_list1 th,.news_list1 td{ border: 1px solid #2d2d2d; height: 30px;}.container_bk .news_list1 .tb_title{ width: 50%;}.container_bk .news_list1 .tb_date{ width: 20%;}.container_bk .news_list1 .tb_detail{ width: 30%;}.container_bk .news_list1 .tb_l{ text-align: left; text-indent: 10px;}.container_bk .news_list1 .tb_c{ text-align: center;}.container_bk .page_list1{ width: 700px; height: auto; color: #2d2d2d; margin: 3px auto; border-bottom: 1px solid #2d2d2d; padding-bottom: 5px; border-collapse: collapse;}.container_bk .page_list1 tr{ height: 40px;}/*-------------------------------------------------------------------------------*/.container_bk .footer{ width: 100%; float: left; height: 50px; font-size: 16px; font-family: "微软雅黑"; font-style: oblique; line-height: 50px; color: #ffffff; background-color: #e6a721;}/*--------添加新闻--------------------------------------------------------------------*/.container_bk .content_bk .newsadd_tb{ width: 100%; border-collapse: collapse;}.container_bk .newsadd_tb td{ height: 35px; line-height: 35px;}.container_bk .newsadd_tb input[name=news_title]{ width: 450px; height: 28px;}.container_bk .newsadd_tb input[name=news_author]{ width: 250px; height: 26px;}.container_bk .newsadd_tb select{ width: 150px; height: 26px;}.container_bk .newsadd_tb input[type=submit],.container_bk .newsadd_tb input[type=reset]{ width: 80px; height: 28px; border: 1px solid green; margin-top: 10px; margin-right: 20px;}/*--------------------------------------------------------------------*/.container_bk .content_bk .typeadd_tb{ width: 100%; border-collapse: collapse;}.container_bk .typeadd_tb td{ height: 35px; line-height: 35px;}.container_bk .typeadd_tb input[name=type_name]{ width: 300px; height: 28px;}.container_bk .typeadd_tb input[type=submit],.container_bk .typeadd_tb input[type=reset]{ width: 80px; height: 28px; border: 1px solid green; margin-top: 10px; margin-right: 20px;}.container_bk .typeadd_tb .tb_l{ width: 15%;}/*---------------------------------------------------------------*/.container_bk .news_type a{ text-decoration: none; color: #2d2d2d;}.container_bk .news_list1 a{ text-decoration: none; color: #2d2d2d;}
23.数据库文件
-- phpMyAdmin SQL Dump-- version 4.8.4-- https://www.phpmyadmin.net/---- 主机: 127.0.0.1-- 生成日期: 2019-03-30 -- 服务器版本: 10.1.37-MariaDB-- PHP 版本: 7.3.1SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";SET AUTOCOMMIT = 0;START TRANSACTION;SET time_zone = "+00:00";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8mb4 */;---- 数据库: `news`--CREATE DATABASE IF NOT EXISTS `news` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `news`;-- ------------------------------------------------------------ 表的结构 `admin`---- 创建时间: 2019-03-26 09:43:13--CREATE TABLE `admin` ( `id` int(11) NOT NULL, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `admin`--INSERT INTO `admin` (`id`, `username`, `password`) VALUES(1, 'admin', '123'),(2, 'root', '456');-- ------------------------------------------------------------ 表的结构 `news`---- 创建时间: 2019-03-26 09:39:24--CREATE TABLE `news` ( `news_id` int(20) NOT NULL, `news_title` varchar(50) NOT NULL, `news_type` varchar(20) NOT NULL, `news_content` text, `news_date` date NOT NULL, `news_author` varchar(20) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `news`--INSERT INTO `news` (`news_id`, `news_title`, `news_type`, `news_content`, `news_date`, `news_author`) VALUES(1, '新闻题目一', '1', '新闻内容一\r\n', '2019-03-27', '作者'),(2, '新闻二', '1', '新闻内容二\r\n', '2019-03-27', '作者'),(3, '新闻三', '1', '新闻内容三', '2019-03-27', '作者'),(4, '新闻四', '1', '新闻内容四', '2019-03-30', '作者'),(5, '新闻五', '1', '新闻内容五', '2019-03-27', '作者'),(6, '新闻六', '1', '新闻内容六', '2019-03-27', '作者'),(7, '新闻七', '1', '新闻内容七', '2019-03-27', '作者'),(8, '新闻八', '1', '新闻内容八', '2019-03-27', '作者'),(9, '新闻九', '1', '新闻内容九', '2019-03-29', '作者'),(10, '新闻十', '1', '新闻内容十', '2019-03-27', '作者'),(11, '新闻十一', '1', '新闻内容十一', '2019-03-30', '作者');-- ------------------------------------------------------------ 表的结构 `newstype`---- 创建时间: 2019-03-26 09:41:25--CREATE TABLE `newstype` ( `type_id` int(11) NOT NULL, `type_name` varchar(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `newstype`--INSERT INTO `newstype` (`type_id`, `type_name`) VALUES(1, '国内新闻'),(2, '国际新闻');---- 转储表的索引------ 表的索引 `admin`--ALTER TABLE `admin` ADD PRIMARY KEY (`id`);---- 表的索引 `news`--ALTER TABLE `news` ADD PRIMARY KEY (`news_id`);---- 表的索引 `newstype`--ALTER TABLE `newstype` ADD PRIMARY KEY (`type_id`);---- 在导出的表使用AUTO_INCREMENT------ 使用表AUTO_INCREMENT `admin`--ALTER TABLE `admin` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;---- 使用表AUTO_INCREMENT `news`--ALTER TABLE `news` MODIFY `news_id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;---- 使用表AUTO_INCREMENT `newstype`--ALTER TABLE `newstype` MODIFY `type_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;COMMIT;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
此数据库文件,有用的部分也就CREATE DATABASE news和CREATE TABLE admin,news,newstype以及INSERT INTO admin,news,newstype。
附带一张banner图片
