1、主页面(main.php)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新闻后台管理系统</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
<style type="text/css">
h2 {text-align: center;margin-top:20px;}
.add {margin-bottom: 20px;}
td {text-align: center;}
</style>
</head>
<body>
<div class="container">
<div class="wrapper">
<h2>新闻后台管理系统</h2>
<div class="add">
<a href="add.html">
<button type="button" class="btn btn-success">增加新闻</button>
</a>
</div>
<table class="table table-light table-hover table-bordered" style="width:100%">
<tr>
<th>ID</th>
<th>标题</th>
<th>关键字</th>
<th>作者</th>
<th>发布时间</th>
<th>内容</th>
<th>操作</th>
</tr>
<?php
// 1.导入配置文件
require "common.php";
//创建一句SQL,获取数据库的表的数据(升序排列)
$_sql = "SELECT * FROM news order by id asc";
//创建一个结果集
$_result = $_mysqli->query($_sql);
// 解析结果集,$row为新闻所有数据,$newsNum为新闻数目
// mysqli_num_rows获取结果中行的数量
$newsNum = mysqli_num_rows($_result);
for($i=0;$i<$newsNum;$i++){
//mysqli_fetch_assoc获取一行结果并以关联数组返回
$row = mysqli_fetch_assoc($_result);
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['keywords']}</td>";
echo "<td>{$row['author']}</td>";
echo "<td>{$row['addtime']}</td>";
echo "<td>{$row['content']}</td>";
echo "<td>
<a href='javascript:del({$row['id']})'>
<button type='button' class='btn btn-danger'>删除</button>
</a>
<a href='update.php?id={$row['id']}'>
<button type='button' class='btn btn-info'>修改</button>
</a>
</td>";
echo "</tr>";
}
//5. 释放结果集
$_result->free();
//关闭连接
$_mysqli->close();
?>
</table>
</div>
</div>
<script type="text/javascript">
function del (id) {
if (confirm("确定删除这条新闻吗?")){
window.location = "delete.php?id="+id;
}
}
</script>
</body>
</html>
2、添加操作界面(add.html)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>添加新闻</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
<style type="text/css">
form{
margin:20px;
}
</style>
</head>
<body>
<form action="add_cl.php" method="post">
<center>
<div class="container">
<h2>添加新闻</h2>
<table class="table table-light table-hover table-bordered" style="width:60%">
<tr><td colspan="2" align="center">新增用户</td></tr>
<tr><td>标题:</td><td><input type="text" name="title"></td></tr>
<tr><td>关键字:</td><td><input type="text" name="keywords"></td></tr>
<tr><td>作者:</td><td><input type="text" name="author"></td></tr>
<tr><td>发布时间:</td><td><input type="date" name="addtime"></td></tr>
<tr><td>内容:</td><td><input type="text" name="content"></td></tr>
<tr><td colspan="2" align="center"><button type="submit" class="btn btn-success">新增</button></td></tr>
</table>
</div>
</center>
</form>
</body>
</html>
3、添加操作执行界面(add.php)
<?php
// 1.导入配置文件
require "common.php";
// 获取增加的新闻
if(isset($_POST['title'])){
$title = $_POST['title'];
}
if(isset($_POST['keywords'])){
$keywords = $_POST['keywords'];
}
if(isset($_POST['author'])){
$author = $_POST['author'];
}
if(isset($_POST['addtime'])){
$addtime = $_POST['addtime'];
}
if(isset($_POST['content'])){
$content = $_POST['content'];
}
//创建sql语句插入数据
$_sql = "insert into `news`(title,keywords,author,addtime,content) values('$title','$keywords','$author','$addtime','$content')";
//创建一个结果集
$_result = $_mysqli->query($_sql);
//5. 释放结果集
//$_result->free();
//关闭连接
//$_mysqli->close();
header("Location:main.php");
?>
4、修改操作界面(update.php)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改新闻</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php
require "common.php";
$id = $_GET['id'];
//根据id获取当前一行数据
$_sql = "SELECT * FROM news WHERE id=$id";
//创建一个结果集
$_result = $_mysqli->query($_sql);
$sql_arr = mysqli_fetch_assoc($_result);
?>
<form action="update_cl.php" method="post">
<center>
<div class="container">
<h2>修改新闻</h2>
<table class="table table-light table-hover table-bordered" style="width:60%">
<tr><td colspan="2" align="center">修改用户</td></tr>
<tr><td><label>新闻ID: </label></td><td><input type="text" name="id" value="<?php echo $sql_arr['id']?>"></td></tr>
<tr><td><label>标题:</label></td><td><input type="text" name="title" value="<?php echo $sql_arr['title']?>"></td></tr>
<tr><td><label>关键字:</label></td><td><input type="text" name="keywords" value="<?php echo $sql_arr['keywords']?>"></td></tr>
<tr><td><label>作者:</label></td><td><input type="text" name="author" value="<?php echo $sql_arr['author']?>"></td></tr>
<tr><td><label>发布时间:</label></td><td><input type="date" name="addtime" value="<?php echo $sql_arr['addtime']?>"></td></tr>
<tr><td><label>内容:</label></td><td><input type="text" name="content" value="<?php echo $sql_arr['content']?>"></td></tr>
<tr><td colspan="2" align="center"><button type="submit" class="btn btn-success">修改</button></td></tr>
<table>
</div>
</center>
</form>
</body>
</html>
5、修改操作执行界面(update_cl.php)
<?php
// 处理编辑操作的页面
require "common.php";
// 获取修改的新闻
if(isset($_POST['title'])){
$title = $_POST['title'];
}
if(isset($_POST['keywords'])){
$keywords = $_POST['keywords'];
}
if(isset($_POST['author'])){
$author = $_POST['author'];
}
if(isset($_POST['addtime'])){
$addtime = $_POST['addtime'];
}
if(isset($_POST['content'])){
$content = $_POST['content'];
}
if(isset($_POST['id'])){
$id = $_POST['id'];
}
//创建sql语句修改数据
$_sql = "UPDATE `news` SET title='$title',keywords='$keywords',author='$author',addtime='$addtime',content='$content' WHERE id='$id'";
//创建一个结果集
$_result = $_mysqli->query($_sql);
if($_result){
echo '修改成功!';
}else{
echo '修改失败';
}
//5. 释放结果集
//$_result->free();
//关闭连接
//$_mysqli->close();
header("Location:main.php");
?>
6、删除操作(delete.php)
<?php
// 处理删除操作的页面
require "common.php";
if(isset($_GET['id'])){
$id = $_GET['id'];
}
//删除指定数据
$_sql="DELETE FROM news WHERE id={$id}";
//创建一个结果集
$_result = $_mysqli->query($_sql);
if($_result){
echo '删除成功!';
}else{
echo '删除失败';
}
// 删除完跳转到新闻页
header("Location:main.php");
?>