PHP+MySQL实现新闻管理系统

PHP+MySQL实现增删改查

这里用PHP和MySQL实现了一个新闻管理系统的增删改查的功能。

一、数据库

首先创建数据库

二、创建项目

1、我是在eclipse里面创建的PHP Project项目,项目目录如下: 这里需要在eclipse里面下载php插件才能创建PHP Project项目,下载插件就是的流程是:运行eclipse,在主界面里找到Help下的“Instal New Software”。然后在Work with中选择“All Available Sites”,到这里加载有些慢,需要耐心等待,然后选择web、xml、java EE、and OSGI…,在其中找到PHP Development Tools (PDT)软件。勾选,点击Next。重启eclipse就行了。 2、创建文件dbconfig.php,连接数据库

<?php
define("HOST", "localhost");
define("USER", "root");
define("PASS", "root");
define("DBNAME", "news");

3、创建主页显示文件index.php

4、增加新闻的页面addnews.php

action-addnews.php

<?php
// 处理增加操作的页面
require "dbconfig.php";
// 连接mysql
$link = @mysql_connect(HOST, USER, PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME, $link);
// 编码设置
mysql_set_charset(utf8, $link);

// 获取增加的新闻
$title = $_POST[title];
$keywords = $_POST[keywords];
$author = $_POST[author];
$addtime = $_POST[addtime];
$content = $_POST[content];
// 插入数据
mysql_query("INSERT INTO tb_news(title,keywords,author,addtime,content) VALUES ($title,$keywords,$author,$addtime,$content)", $link) or die(添加数据出错: . mysql_error());
header("Location:index.php");

5、删除新闻 action-del.php

<?php
// 处理删除操作的页面
require "dbconfig.php";
// 连接mysql
$link = @mysql_connect(HOST, USER, PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME, $link);
// 编码设置
mysql_set_charset(utf8, $link);

$id = $_GET[id];
// 删除指定数据
mysql_query("DELETE FROM tb_news WHERE id={$id}", $link) or die(删除数据出错: . mysql_error());
// 删除完跳转到新闻页
header("Location:index.php");

6、修改新闻内容 editnews.php

action-editnews.php

<?php
// 处理编辑操作的页面
require "dbconfig.php";
// 连接mysql
$link = @mysql_connect(HOST, USER, PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME, $link);
// 编码设置
mysql_set_charset(utf8, $link);

// 获取修改的新闻
$id = $_POST[id];
$title = $_POST[title];
$keywords = $_POST[keywords];
$author = $_POST[author];
$addtime = $_POST[addtime];
$content = $_POST[content];
// 更新数据
mysql_query("UPDATE tb_news SET title=$title,keywords=$keywords,author=$author,addtime=$addtime,content=$content WHERE id=$id", $link) or die(修改数据出错: . mysql_error());
header("Location:index.php");

最后运行就好了,我的地址是http://localhost/PHPProject/index.php,我的项目是放在Apache服务自动配置的运行目录下的,运行起来Apache服务之后直接访问就好。

经验分享 程序员 微信小程序 职场和发展