在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");

标签: none

已有 8 条评论

  1. 新车首发,新的一年,只带想赚米的人coinsrore.com

  2. 2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

  3. 2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

  4. 华纳总公司开户申请步骤?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】

  5. 华纳东方明珠客服电话是多少?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠开户专线联系方式?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    如何联系华纳东方明珠客服?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠官方客服联系方式?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠客服热线?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠开户客服电话?(▲182(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠24小时客服电话?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠客服邮箱?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠官方客服在线咨询?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】
    华纳东方明珠客服微信?(▲18288362750?《?微信STS5099? 】【╃q 2704132802╃】

  6. 新盛客服电话是多少?(?183-8890-9465—《?薇-STS5099】【
    新盛开户专线联系方式?(?183-8890--9465—《?薇-STS5099】【?扣6011643??】
    新盛客服开户电话全攻略,让娱乐更顺畅!(?183-8890--9465—《?薇-STS5099】客服开户流程,华纳新盛客服开户流程图(?183-8890--9465—《?薇-STS5099】

  7. 果博东方客服开户联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方公司客服电话联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方开户流程【182-8836-2750—】?薇- cxs20250806】
    果博东方客服怎么联系【182-8836-2750—】?薇- cxs20250806】

  8. 果博东方客服开户联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方公司客服电话联系方式【182-8836-2750—】?薇- cxs20250806】
    果博东方开户流程【182-8836-2750—】?薇- cxs20250806】
    果博东方客服怎么联系【182-8836-2750—】?薇- cxs20250806】

添加新评论