优化 数据库连接

This commit is contained in:
LyLme 2022-03-12 15:37:29 +08:00
parent 9507e13d71
commit b869f1b140
1 changed files with 79 additions and 96 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
$title = '网站链接管理'; $title = '网站链接管理';
include './head.php'; include './head.php';
$grouplists =mysqli_query($con,"SELECT * FROM `lylme_groups`"); $grouplists = $DB->query("SELECT * FROM `lylme_groups`");
?> ?>
<main class="lyear-layout-content"> <main class="lyear-layout-content">
@ -12,11 +12,8 @@ $grouplists =mysqli_query($con,"SELECT * FROM `lylme_groups`");
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<?php <?php
$set = isset($_GET['set']) ? $_GET['set'] : null; $set = isset($_GET['set']) ? $_GET['set'] : null;
if ($set == 'add') {
if($set=='add')
{
echo '<h4>新增链接</h4> echo '<h4>新增链接</h4>
<div class="panel-body"> <div class="panel-body">
<form action="./link.php?set=add_submit" method="POST"> <form action="./link.php?set=add_submit" method="POST">
@ -37,9 +34,12 @@ echo '<h4>新增链接</h4>
<div class="form-group"> <div class="form-group">
<label>*分组:</label><br> <label>*分组:</label><br>
<select class="form-control" name="group_id">'; <select class="form-control" name="group_id">';
while ($grouplist = $DB->fetch($grouplists)) {
while($grouplist = mysqli_fetch_assoc($grouplists)) { if ($grouplist["group_id"] == $row['group_id']) {
if($grouplist["group_id"]==$row['group_id']){$select='selected="selected"';}else {$select='';} $select = 'selected="selected"';
} else {
$select = '';
}
echo '<option value="' . $grouplist["group_id"] . '">' . $grouplist["group_id"] . ' - ' . $grouplist["group_name"] . '</option>'; echo '<option value="' . $grouplist["group_id"] . '">' . $grouplist["group_id"] . ' - ' . $grouplist["group_name"] . '</option>';
} }
echo '</select></div> echo '</select></div>
@ -48,12 +48,10 @@ echo '</select></div>
</div> </div>
<br/><a href="./link.php"><<返回</a> <br/><a href="./link.php"><<返回</a>
</div></div>'; </div></div>';
} } elseif ($set == 'edit') {
elseif($set=='edit')
{
$id = $_GET['id']; $id = $_GET['id'];
$row2 = mysqli_query($con,"select * from lylme_links where id='$id' limit 1"); $row2 = $DB->query("select * from lylme_links where id='$id' limit 1");
$row=mysqli_fetch_assoc($row2); $row = $DB->fetch($row2);
echo '<h4>修改链接信息</h4> echo '<h4>修改链接信息</h4>
<div class="panel-body"> <div class="panel-body">
<form action="./link.php?set=edit_submit&id=' . $id . '" method="POST"> <form action="./link.php?set=edit_submit&id=' . $id . '" method="POST">
@ -75,8 +73,12 @@ echo '<h4>修改链接信息</h4>
<div class="form-group"> <div class="form-group">
<label>*分组:</label><br> <label>*分组:</label><br>
<select class="form-control" name="group_id">'; <select class="form-control" name="group_id">';
while($grouplist = mysqli_fetch_assoc($grouplists)) { while ($grouplist = $DB->fetch($grouplists)) {
if($grouplist["group_id"]==$row['group_id']){$select='selected="selected"';}else {$select='';} if ($grouplist["group_id"] == $row['group_id']) {
$select = 'selected="selected"';
} else {
$select = '';
}
echo '<option value="' . $grouplist["group_id"] . '" ' . $select . '>' . $grouplist["group_id"] . ' - ' . $grouplist["group_name"] . '</option>'; echo '<option value="' . $grouplist["group_id"] . '" ' . $select . '>' . $grouplist["group_id"] . ' - ' . $grouplist["group_name"] . '</option>';
} }
echo '</select> echo '</select>
@ -86,9 +88,7 @@ echo '</select>
</div> </div>
<br/><a href="./link.php"><<返回</a> <br/><a href="./link.php"><<返回</a>
</div></div>'; </div></div>';
} } elseif ($set == 'add_submit') {
elseif($set=='add_submit')
{
$name = $_POST['name']; $name = $_POST['name'];
$url = $_POST['url']; $url = $_POST['url'];
$icon = $_POST['icon']; $icon = $_POST['icon'];
@ -98,20 +98,15 @@ if($name==NULL or $url==NULL){
echo '<script>alert("保存错误,请确保带星号的都不为空!");history.go(-1);</script>'; echo '<script>alert("保存错误,请确保带星号的都不为空!");history.go(-1);</script>';
} else { } else {
$sql = "INSERT INTO `lylme_links` (`id`, `name`, `group_id`, `url`, `icon`, `PS`,`link_order`) VALUES (NULL, '" . $name . "', '" . $group_id . "', '" . $url . "', '" . $icon . "', '" . $name . "', '" . $link_order . "');"; $sql = "INSERT INTO `lylme_links` (`id`, `name`, `group_id`, `url`, `icon`, `PS`,`link_order`) VALUES (NULL, '" . $name . "', '" . $group_id . "', '" . $url . "', '" . $icon . "', '" . $name . "', '" . $link_order . "');";
if ($DB->query($sql)) {
if(mysqli_query($con,$sql)){
echo '<script>alert("添加链接 ' . $name . ' 成功!");window.location.href="/admin/link.php";</script>'; echo '<script>alert("添加链接 ' . $name . ' 成功!");window.location.href="/admin/link.php";</script>';
}else } else echo '<script>alert("添加链接失败!");history.go(-1);</script>';
echo '<script>alert("添加链接失败!");history.go(-1);</script>';
} }
} } elseif ($set == 'edit_submit') {
elseif($set=='edit_submit')
{
$id = $_GET['id']; $id = $_GET['id'];
$rows2 = mysqli_query($con,"select * from lylme_links where id='$id' limit 1"); $rows2 = $DB->query("select * from lylme_links where id='$id' limit 1");
$rows=mysqli_fetch_assoc($rows2); $rows = $DB->fetch($rows2);
if(!$rows) if (!$rows) echo '<script>alert("当前记录不存在!");history.go(-1);</script>';
echo '<script>alert("当前记录不存在!");history.go(-1);</script>';
$name = $_POST['name']; $name = $_POST['name'];
$url = $_POST['url']; $url = $_POST['url'];
$icon = $_POST['icon']; $icon = $_POST['icon'];
@ -119,37 +114,27 @@ $group_id=$_POST['group_id'];
if ($name == NULL or $url == NULL) { if ($name == NULL or $url == NULL) {
echo '<script>alert("保存错误,请确保带星号的都不为空!");history.go(-1);</script>'; echo '<script>alert("保存错误,请确保带星号的都不为空!");history.go(-1);</script>';
} else { } else {
$sql = "UPDATE `lylme_links` SET `name` = '" . $name . "', `url` = '" . $url . "', `icon` = '" . $icon . "', `group_id` = '" . $group_id . "' WHERE `lylme_links`.`id` = '" . $id . "';"; $sql = "UPDATE `lylme_links` SET `name` = '" . $name . "', `url` = '" . $url . "', `icon` = '" . $icon . "', `group_id` = '" . $group_id . "' WHERE `lylme_links`.`id` = '" . $id . "';";
if(mysqli_query($con,$sql)) if ($DB->query($sql)) echo '<script>alert("修改链接 ' . $name . ' 成功!");window.location.href="/admin/link.php";</script>';
echo '<script>alert("修改链接 '.$name.' 成功!");window.location.href="/admin/link.php";</script>'; else echo '<script>alert("修改链接失败!");history.go(-1);</script>';
else
echo '<script>alert("修改链接失败!");history.go(-1);</script>';
} }
} } elseif ($set == 'delete') {
elseif($set=='delete')
{
$id = $_GET['id']; $id = $_GET['id'];
$sql = "DELETE FROM lylme_links WHERE id='$id'"; $sql = "DELETE FROM lylme_links WHERE id='$id'";
if(mysqli_query($con,$sql)) if ($DB->query($sql)) echo '<script>alert("删除成功!");window.location.href="/admin/link.php";</script>';
echo '<script>alert("删除成功!");window.location.href="/admin/link.php";</script>'; else echo '<script>alert("删除失败!");history.go(-1);</script>';
else } else {
echo '<script>alert("删除失败!");history.go(-1);</script>';
}
else
{
echo '<div class="alert alert-info">系统共有 <b>' . $linksrows . '</b> 个链接<br/><a href="./link.php?set=add" class="btn btn-primary">新增链接</a></div> echo '<div class="alert alert-info">系统共有 <b>' . $linksrows . '</b> 个链接<br/><a href="./link.php?set=add" class="btn btn-primary">新增链接</a></div>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-striped"> <table class="table table-striped">
<thead><tr><th>序号</th><th>名称</th><th>链接</th><th>分组</th><th>操作</th></tr></thead> <thead><tr><th>序号</th><th>名称</th><th>链接</th><th>分组</th><th>操作</th></tr></thead>
<tbody>'; <tbody>';
$i = 0; $i = 0;
$rs=mysqli_query($con,"SELECT * FROM `lylme_links` ORDER BY `lylme_links`.`id` ASC"); $rs = $DB->query("SELECT * FROM `lylme_links` ORDER BY `lylme_links`.`id` ASC");
while($res = mysqli_fetch_array($rs)) while ($res = $DB->fetch($rs)) {
{
$i = $i + 1; $i = $i + 1;
echo '<tr><td><b>' . $i . '</b></td><td>' . $res['name'] . '</td><td>' . $res['url'] . '</td><td>'; echo '<tr><td><b>' . $i . '</b></td><td>' . $res['name'] . '</td><td>' . $res['url'] . '</td><td>';
echo mysqli_fetch_assoc(mysqli_query($con,"SELECT * FROM `lylme_groups` WHERE `group_id` = ".$res['group_id']))["group_name"]; echo $DB->fetch($DB->query("SELECT * FROM `lylme_groups` WHERE `group_id` = " . $res['group_id'])) ["group_name"];
echo '</td><td><a href="./link.php?set=edit&id=' . $res['id'] . '" class="btn btn-info btn-xs">编辑</a>&nbsp;<a href="./link.php?set=delete&id=' . $res['id'] . '" class="btn btn-xs btn-danger" onclick="return confirm(\'删除 ' . $res['name'] . ' \');">删除</a></td></tr>'; echo '</td><td><a href="./link.php?set=edit&id=' . $res['id'] . '" class="btn btn-info btn-xs">编辑</a>&nbsp;<a href="./link.php?set=delete&id=' . $res['id'] . '" class="btn btn-xs btn-danger" onclick="return confirm(\'删除 ' . $res['name'] . ' \');">删除</a></td></tr>';
} }
?> ?>
@ -166,8 +151,6 @@ echo '</td><td><a href="./link.php?set=edit&id='.$res['id'].'" class="btn btn-in
</main> </main>
<?php <?php
} }
include './footer.php'; include './footer.php';
?> ?>