PHP实战 新闻管理系统 使用到了bootstrap框架
刚刚接触 PHP 仿照视频 写了个新闻管理系统 其中也使用到了bootstrap框架
写下来整理一下思路。
这是个很简单的系统,首先是建立数据库表。
mysql>create database newsdb
mysql> create table news(
-> id int unsigned not null auto_increment primary key,//这是新闻的id
-> title varchar(64) not null,//这是新闻的标题
-> keywords varchar(64) not null,//这是新闻的关键字
-> author varchar(16) not null,//这是新闻的作者
-> addtime int unsigned not null,//这是新闻的添加时间
-> content text not null);//这是新闻的内容
这样,数据库表就建成了,下面开始写页面。
首先写了一个数据库配置文件dbconfig.php:
define(HOST,"localhost");//主机名
define(USER,"root");//用户名
define(PASS,"");//密码
define(DBNAME,"newsdb");//数据库名
?>
然后是一个menu.php文件
上面两步简单的工作做好之后,就该进行主页http://blog.csdn.net/q114942784/article/details/index.php的编写了:
首先,导入导航栏menu.php
然后是加个标题和表格
浏览新闻
新闻id | 标题 | 关键字 | 作者 | 时间 | 内容 | 操作 |
---|---|---|---|---|---|---|
{$row['id']} | ";{$row['tilte']} | ";{$row['keywords']} | ";{$row['author']} | ";{$row['addtime']} | ";{$row['content']} | ";
删除;//此处的“#”只是一个代号,后面会把它替换掉,由于增删操作比较复杂,所以单独写一个action.php文件 修改; | ";
action.php:
//这是一个数据的增删改查的页面
//1.导入配置文件
require("dbconfig.php");
//2.链接mysql,并选择数据库
$link=@mysql_connect(HOST,USER,PASS) or die("数据库链接失败");
mysql_select_db(DBNAME,$link);
//3.根据action的值,来判断所属的操作,执行相应的代码
switch($_GET["action"]){
case"add":
//1.获取要添加的信息,补充其他信息
$tilte=$_POST["title"];
$keywords=$_POST["keywords"];
$author=$_POST["author"];
$content=$_POST["content"];
$addtime=time();
//2.信息的过滤
//3.拼接sql语句,执行相应的操作
$sql=insert into news value(null,'($title)','($keywords)','($author)',$addtime,'($content)');
mysql_query($sql,$link);
//4.判断是否成功
$id=mysql_insert_id($link);
if($id>0){
echo "
新闻信息添加成功
";}
else{
echo "
新闻信息添加失败
";}
echo("返回");
echo("浏览新闻");
break;
case "del":
//1.获取要删除的新闻id:
$id=$_GET['id'];
//2.拼装删除sql语句,执行相应的删除操作
$sql="delete from news where id=($id)";
mysql_query($sql,$link);
//3.删除之后自动跳转至新闻浏览界面
header("location:http://blog.csdn.net/q114942784/article/details/index.php");
break;
case "update":
//1.获取要修改的信息
$title = $_POST['title'];
$keywords = $_POST['keywords'];
$author = $_POST['author'];
$content = $_POST['content'];
$id = $_POST['id'];
//2.过滤要修改的信息(此处省略)
//3.拼装修改sql语句,并执行修改操作
$sql="update news set title="($title)",keywords='($keywords)',author='($author)',content='($content)' where id=($id)";
//echo $sql;
mysql_query($sql,$link);
//4.跳转至浏览界面
header("location:http://blog.csdn.net/q114942784/article/details/index.php");
break;
}
//4.关闭数据库链接
mysql_close("$link");
?>
下面写添加新闻的页面http://blog.csdn.net/q114942784/article/details/add.php文件:
发布新闻
然后是编辑的页面edit.php页面:
//1.导入配置文件
require("dbconfig.php");
//2.连接mysql,选择数据库
$link=@mysql_connect(HOST,USER,PASS)or die("数据库链接失败");
mysql_select_db(DBNAME,$link);
//3.获取要修改的信息的id,并且拼装查看sql语句,执行查询,获取要修改信息
$sql="select * from news where id={$_GET['id']}";
$result=mysql_query($sql,$link);
//4.判断是否获取到了要修改的信息
if($result && mysql_num_rows($result)>0){
$news=mysql_fetch_assoc($result);
}else{
die("没有找到要修改的信息");
}
?>
编辑新闻
最后,提一下,删除和修改的“#”用什么代替
此处为了人性化一些,用js代码给出一个提示
第一个“#”,用javascript:dodel({$row["id"]})替代
第二个“#”,用edit.php?id={$row["id"]}替代
至此,一个完整的php新闻管理系统就基本完成了,明天再改进一下。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
