前言
数据库实验有个思考题挺麻烦的。。。记录一下吧
思考题:建立实验一指导书相关数据的数据库,并用一个页面显示所有的表名,即要求用户点击该表名显示出该表所有记录。并完成插入记录,删除记录以及修改记录的功能,而且数据库中信息随网页端操作而更新。
注:所有的思考题均要求网页代码和运行后的界面。注意界面设计及美观性。(简要 写一下注释以及每部分代码实现的功能)
实验1数据库结构:两张表,分别是 emp+学号,dept+学号
其中dept表表示部门,它的数据如下:
emp表表示员工,它的数据如下:
其实就是一个简单的增删改查,要注意的是表和数据字段的选择,其他的照葫芦画瓢就行了。。。不是很难
前后台结构
总的结构,分为下面这几个页面:
index表示总的表选择界面,然后tableInfo表示表的详情页,后面是三个后台,分别对应添加,删除,修改。
其中 index 给出所有的表,而点击这些表项,可以进入表的详情页 tableInfo, tableInfo 提供三种操作,分别对应三个后台。
数据库连接
创建conn.php文件以便复用连接到数据库的代码:
<?php
$hostname = "localhost";
$database = "lab1";
$username = "root";
$password = "";
$conn = mysqli_connect($hostname, $username, $password);
$db = mysqli_select_db($conn, $database);
?>
在之后的编程中,通过include引入即可复用此代码:
include("conn.php");
表选择界面
首先编写 index.php,通过两个超链接提交表名并且跳转到tableInfo详情页:
<table border="2">
<?php
include("conn.php");
$res = mysqli_query($conn, "show tables");
if(!$res) die("no tables");
echo '<tr><td>数据库lab1的数据表:</td></tr>';
$row = mysqli_num_rows($res);
for($i=0; $i<$row; $i++) {
$dbrow = mysqli_fetch_array($res);
$tableName = $dbrow[0];
$tr = '<a href="tableInfo.php?tableName=' . $tableName . '">'. $tableName . '</a>';
echo "<tr><td>" . $tr . "</td></tr>";
}
?>
</table>
<style>td {
text-align: center;}</style>
浏览器进入 index.php:
表详情页
然后编写 tableInfo 即表详情页,在此页我们查询所有记录并且打印,同时动态 地创建他们的修改删除和添加的链接:
<form action="add.php" method="GET">
<table border="2">
<?php
if(!isset($_GET["tableName"])) die("未选择数据表");
include("conn.php");
$tableName = $_GET["tableName"];
// 打印表头
$columns = array();
$res = mysqli_query($conn, "show columns from " . $tableName);
if(!$res) die("no data");
$row = mysqli_num_rows($res);
echo '<tr style="background-color:red">';
for($i=0; $i<$row; $i++) {
$dbrow = mysqli_fetch_array($res);
echo '<td>' . $dbrow[0] . '</td>';
array_push($columns, $dbrow[0]);
}
echo '<td>操作</td>';
echo '</tr>';
// 打印数据
$res = mysqli_query($conn, "select * from " . $tableName);
$row = mysqli_num_rows($res);
for($i=0; $i<$row; $i++) {
echo '<tr>';
$dbrow = mysqli_fetch_array($res);
for($j=0; $j<count($columns); $j++) {
echo '<td>' . $dbrow[$columns[$j]] . '</td>';
}
echo '<td><a href="alter.php?tableName=' . $tableName . '&primaryKeyName=' . $columns[0] . '&primaryKeyValue=' .$dbrow[0] . '">修改</a> ';
echo '<a href="delete.php?tableName=' . $tableName . '&primaryKeyName=' . $columns[0] . '&primaryKeyValue=' .$dbrow[0] . '">删除</a></td>';
echo '</tr>';
}
// 添加数据
echo '<tr style="background-color:red">';
for($j=0; $j<count($columns); $j++) {
echo '<td><input type="text" name="' . $columns[$j] . '" style="width:120;"></input></td>';
}
echo '<td><input type="submit" style="width:120"; value="添加记录"></td>';
echo '<input type="text" name="tableName" style="display:none;" value=' . $tableName . '></input>';
echo '</tr>';
?>
</table>
</form>
<style>td {
text-align: center;}</style>
然后通过 index 进入两张表的详情页:
修改数据
接下来我们编写 alter.php 完成对记录的修改。首先明确修改一条记录需要的几 样信息:表名,主键名,主键值。
我们在 tableInfo.php 中正是通过 GET 提交来 传递这几样信息。我们获取当前记录的信息,随后我们将表单中的修改后的信息 发送到 alter.bks.php 中,完成真正的修改。
alter.php的代码如下:
<form action="alter.bks.php" method="GET">
<table border="2">
<?php
include("conn.php");
if(!isset($_GET["tableName"])) die("loss key infomation");
if(!isset($_GET["primaryKeyName"])) die("loss key infomation");
if(!isset($_GET["primaryKeyValue"])) die("loss key infomation");
$tableName = $_GET["tableName"];
$primaryKeyName = $_GET["primaryKeyName"];
$primaryKeyValue = $_GET["primaryKeyValue"];
// 打印表头
$columns = array();
$res = mysqli_query($conn, "show columns from " . $tableName);
if(!$res) die("no data");
$row = mysqli_num_rows($res);
echo '<tr style="background-color:red">';
for($i=0; $i<$row; $i++) {
$dbrow = mysqli_fetch_array($res);
echo '<td>' . $dbrow[0] . '</td>';
array_push($columns, $dbrow[0]);
}
echo '<td>操作</td>';
echo '</tr>';
// 查询该记录
$res = mysqli_query($conn, "select * from " . $tableName . ' where ' . $primaryKeyName . '=' . $primaryKeyValue);
if(!$res) die("not found");
$data = mysqli_fetch_assoc($res);
// 修改的数据字段
echo '<tr>';
for($j=0; $j<count($columns); $j++) {
echo '<td><input type="text" name="' . $columns[$j] . '" style="width:120;" value=' . $data[$columns[$j]] . '></input></td>';
}
echo '<td><input type="submit" style="width:120"; value="修改"></td>';
echo '</tr>';
// 隐藏表 -- 提交表名 主键名 主键值
echo '<input type="text" name="tableName" style="display:none;" value=' . $tableName . '></input>';
echo '<input type="text" name="primaryKeyName" style="display:none;" value=' . $primaryKeyName . '></input>';
echo '<input type="text" name="primaryKeyValue" style="display:none;" value=' . $primaryKeyValue . '></input>';
?>
</table>
</form>
<style>td {
text-align: center;}</style>
随后,我们编写 alter.bks.php 的代码,从 get 提交中获取数据,并且构造 sql 语句, 完成对数据库的更新:
<?php
include("conn.php");
$tableName = $_GET["tableName"];
$primaryKeyName = $_GET["primaryKeyName"];
$primaryKeyValue = $_GET["primaryKeyValue"];
// 获取字段名并且构造sql语句
$query = 'update ' . $tableName . ' set ';
$res = mysqli_query($conn, "show columns from " . $tableName);
$row = mysqli_num_rows($res);
for($i=0; $i<$row; $i++) {
$cname = mysqli_fetch_array($res)[0];
$query = $query . $cname . '="' . $_GET[$cname] . '",';
}
$query = substr($query, 0, strlen($query)-1); // 去掉,号
$query = $query . ' where ' . $primaryKeyName . '=' . $primaryKeyValue;
// 执行修改sql
$res = mysqli_query($conn, $query);
if($res)
echo "<script>window.alert('修改成功,请返回');history.back(1);</script>";
else
echo "<script>window.alert('修改失败,请返回');history.back(1);</script>";
?>
以 dept 表为例,我们演示修改数据的过程。首先我们点击修改,进入修改页面:
我们将 LOC 修改为上海:
提示修改成功:
回到表格详情页,信息已经被修改:
删除数据
删除数据记录:我们仍然需要知晓被删除记录的三个属性:表名,主键名,主键 值。我们获取这些值并且直接执行删除语句。下面编写 delete.php 的代码:
<?php
include("conn.php");
$tableName = $_GET["tableName"];
$primaryKeyName = $_GET["primaryKeyName"];
$primaryKeyValue = $_GET["primaryKeyValue"];
$query = 'delete from ' . $tableName . ' where ' . $primaryKeyName . '=' . $primaryKeyValue;
// 执行修改sql
$res = mysqli_query($conn, $query);
if($res)
echo "<script>window.alert('删除成功,请返回');history.back(1);</script>";
else
echo "<script>window.alert('删除失败,请返回');history.back(1);</script>";
?>
我们试着删除第一条数据记录,他的名字是 GREEN:
跳转到删除界面:
刷新之后发现确实删除成功了。找不到名为 GREEN 的记录:
添加数据
添加记录:我们需要知晓添加的表名称,同时,我们需要获取利用表单传递的字 段数据,然后执行插入语句即可。下面是 add.php 的代码:
<?php
include("conn.php");
$tableName = $_GET["tableName"];
// 获取字段名并且构造sql语句
$query = 'insert into ' . $tableName . ' (';
$res = mysqli_query($conn, "show columns from " . $tableName);
$row = mysqli_num_rows($res);
$columns = array(); // 记录列名称
for($i=0; $i<$row; $i++) {
$cname = mysqli_fetch_array($res)[0];
array_push($columns, $cname);
$query = $query . $cname . ',';
}
$query = substr($query, 0, strlen($query)-1); // 去掉,号
$query = $query . ') values (';
for($i=0; $i<count($columns); $i++) {
$cname = $columns[$i];
$query = $query . '"' . $_GET[$cname] . '",';
}
$query = substr($query, 0, strlen($query)-1); // 去掉,号
$query = $query . ')';
// 执行修改sql
$res = mysqli_query($conn, $query);
if($res)
echo "<script>window.alert('添加成功,请返回');history.back(1);</script>";
else
echo "<script>window.alert('添加失败,请返回');history.back(1);</script>";
?>
测试:
点击添加:
GREEN 又回来了。。。
转载:https://blog.csdn.net/weixin_44176696/article/details/109447042