在web端处理数据库信息要的快捷性更好一些,所有本文针对php7对之前php5的代码做了相关改进,以实现对数据库的增加,删除及更改等功能,在后面的版本中还会增加其适应能力,以及数据库自行识别能力。
以下时相关代码,原php5代码可以参考前辈大佬的点击进入
1.0版代码如下:
主界面:
1 2 3 4 5 6 7 8
| <?php include_once("model.php");//引用处理类文件 $f=new ht(); $f -> h_head(); $f -> h_table(); $f -> h_footer();
?>
|
主界面模板文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| <?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>"; } }
}
?>
|
添加界面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!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>
|
添加操作执行代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?php
require "dbconfig.php";
$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");
|
删除操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?php
require "dbconfig.php";
$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"); ?>
|
修改操作:
界面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <!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>
|
执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?php
require "dbconfig.php";
$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");
|
数据库关键信息写入页:
1 2 3 4 5
| <?php define("HOST","localhost"); define("USER","root"); define("PASS","root"); define("DBNAME","test");
|