基于php7的简单web端MySQL后台
在web端处理数据库信息要的快捷性更好一些,所有本文针对php7对之前php5的代码做了相关改进,以实现对数据库的增加,删除及更改等功能,在后面的版本中还会增加其适应能力,以及数据库自行识别能力。
以下时相关代码,原php5代码可以参考前辈大佬的点击进入
1.0版代码如下:
主界面:
<?php
include_once("model.php");//引用处理类文件
$f=new ht();
$f -> h_head();
$f -> h_table();
$f -> h_footer();
?>
主界面模板文件:
<?php
// model.php(程序处理类)
Class ht{
function h_head(){
echo <<< html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>西电导航后台管理系统</title>
</head>
<style type="text/css">
.wrapper {width: 1000px;margin: 20px auto;}
h2 {text-align: center;}
.add {margin-bottom: 20px;}
.add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;}
td {text-align: center;}
table {
table-layout: fixed;
word-wrap:break-word;
}
</style>
<body>
<div class="wrapper">
<h2>西电导航数据库后台管理系统</h2>
<div class="add">
<a href="addnews.html">增加条目</a>
</div>
<table width="960" border="1" >
<tr>
<th>标题</th>
<th>url</th>
<th>发布时间</th>
<th>内容</th>
<th>操作</th>
</tr>
html;
}
function h_footer(){
echo <<< html
</table>
</div>
</div>
<script type="text/javascript">
function del (id) {
if (confirm("确定删除这条新闻吗?")){
window.location = "action-del.php?title="+id;
}
}
</script>
</body>
</html>
html;
}
function h_table(){
header("Content-type: text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$conn = new mysqli($servername, $username, $password,"test");
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
mysqli_set_charset($conn,'utf8');
// 3. 从DBNAME中查询到news数据库,返回数据库结果集,并按照addtime降序排列
// 结果集
$result = mysqli_query($conn,"SELECT * FROM bs_3");
// var_dump($result);die;
// 解析结果集,$row为新闻所有数据,$newsNum为新闻数目
$newsNum=mysqli_num_rows($result);
for($i=0; $i<$newsNum; $i++){
$row = mysqli_fetch_assoc($result);
echo "<tr>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['url']}</td>";
echo "<td>{$row['t_date']}</td>";
echo "<td>{$row['page']}</td>";
echo "<td>
<a href='javascript:del(\"{$row['title']}\")'>删除</a>
<a href='editnews.php?id={$row['title']}'>修改</a>
</td>";
echo "</tr>";
}
}
}
?>
添加界面:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>添加内容</title>
</head>
<style type="text/css">
form{
margin: 20px;
}
</style>
<body>
<form action="action-addnews.php" method="post">
<label>标题:</label><input type="text" name="title">
<label>url:</label><input type="text" name="url">
<label>发布时间:</label><input type="date" name="t_date">
<label>内容:</label><input type="text" name="page">
<input type="submit" value="提交">
</form>
</body>
</html>
添加操作执行代码:
<?php
// 处理增加操作的页面
require "dbconfig.php";
// 连接mysql
$link = @mysqli_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysqli_select_db($link,DBNAME);
// 编码设置
mysqli_set_charset($link,'utf8');
// 获取增加的新闻
$title = $_POST['title'];
$url = $_POST['url'];
$t_date = $_POST['t_date'];
$page = $_POST['page'];
// 插入数据
mysqli_query($link,"INSERT INTO bs_3(title,url,t_date,page) VALUES ('$title','$url','$t_date','$page')") or die('添加数据出错!');
header("Location:index.php");
删除操作:
<?php
// 处理删除操作的页面
require "dbconfig.php";
// 连接mysql
$link = @mysqli_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysqli_select_db($link,DBNAME);
// 编码设置
mysqli_set_charset($link,'utf8');
$title = $_GET['title'];
echo $title;
$title=str_replace('"','',$title);
//删除指定数据
mysqli_query($link,"DELETE FROM bs_3 WHERE title='$title'")or die("提示:删除失败");
// 删除完跳转到新闻页
echo $title;
header("Location:index.php");
?>
修改操作:
界面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改数据库内容</title>
</head>
<body>
<?php
require "dbconfig.php";
$link = @mysqli_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
mysqli_select_db($link,DBNAME);
mysqli_set_charset($link,'utf8');
$id = $_GET['id'];
$sql = mysqli_query($link,"SELECT * FROM bs_3 WHERE title='$id'");
echo "请谨慎修改";
$sql_arr = mysqli_fetch_assoc($sql);
?>
<form action="action-editnews.php" method="post">
<label>标题:</label><input type="text" name="title" value="<?php echo $sql_arr['title']?>">
<label>url:</label><input type="text" name="url" value="<?php echo $sql_arr['url']?>">
<label>发布时间:</label><input type="date" name="t_date" value="<?php echo $sql_arr['t_date']?>">
<label>内容:</label><input type="text" name="page" value="<?php echo $sql_arr['page']?>">
<input type="submit" value="提交">
</form>
</body>
</html>
执行:
<?php
// 处理编辑操作的页面
require "dbconfig.php";
// 连接mysql
$link = @mysqli_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysqli_select_db($link,DBNAME);
// 编码设置
mysqli_set_charset($link,'utf8');
// 获取修改的新闻
$title = $_POST['title'];
$url = $_POST['url'];
$t_date = $_POST['t_date'];
$page = $_POST['page'];
// 更新数据
mysqli_query($link,"UPDATE bs_3 SET title='$title',url='$url',t_date='$t_date',page='$page' WHERE title='$title'") or die('修改数据出错:');
header("Location:index.php");
数据库关键信息写入页:
<?php
define("HOST","localhost");
define("USER","root");
define("PASS","root");
define("DBNAME","test");