查询输出页面
具体的内容可以看我昨天的博客
今天只需在操作的一列下添加修改和删除以及添加的三个操作超链接即可
//table.php
//主要是看超链接处如何传递id的值
<html>
<head>
<meta charset="utf-8">
<title>员工信息表</title>
<style >
.box{margin:40;}
</style>
</head>
<body>
<div class="box" align="center">
<div class="title" align="center">员工信息表</div>
<table>
<tr>
<a href="./insert.php">添加员工信息</a>
</tr>
<tr>
<!-- <th width="5%"></th> -->
<th>员工编号</th>
<th>员工姓名</th>
<th>出生日期</th>
<th>工作部门</th>
<th>入职日期</th>
<th>操作</th>
</tr>
<tr>
<?php foreach($data as $row) {?>
</tr>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['birthday']; ?></td>
<td><?php echo $row['department']; ?></td>
<td><?php echo $row['entry_time']; ?></td>
<!-- <td><a href="<?php echo './updatework.php?id='.$row['id']; ?>">修改</a></td> -->
<td><a href="<?php echo './update2.php?id='.$row['id']; ?>">修改</a></td>
<td><a href="<?php echo './deletework.php?id='.$row['id']; ?>">删除</a></td>
</tr>
<?php }?>
</table>
<div class="page"><?php echo $page_html; ?></div>
</div>
</body>
</html>
插入数据
在插入数据和修改数据时,从文本框中输入的为日期,需要转换为时间戳存储
//insert.php
<html>
<head>
<meta charset="utf-8">
<title>添加员工信息</title>
</head>
<body>
<div class="box" align="center">
<form action="doinsert.php" method="post">
<table>
<tr>
<h2>添加员工信息</h2>
</tr>
<tr>
<th>姓名:</th>
<td><input type="text" name="name" value=""/></td>
</tr>
<tr>
<th>生日:</th>
<td><input type="text" name="birthday" value=""/></td>
</tr>
<tr>
<th>部门:</th>
<th><input type="text" name="department" value=""/></th>
</tr>
<tr>
<th>入职时间:</th>
<th><input type="text" name="entry_time" value=""/></th>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
<input type="reset" value="重新编辑"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
//doinsert.php
<?php
header('Content-Type: text/html; charset=utf-8');
$connect = new mysqli('localhost','root','root','test');
$connect->query("set names utf8");
$name=$_POST["name"];
$birthday=strtotime($_POST["birthday"]);
$department=$_POST["department"];
$entry_time=strtotime($_POST["entry_time"]);
$sql_insert = "insert into workinfo (name,birthday,department,entry_time) value ('$name','$birthday','$department','$entry_time')";
$res = $connect->query($sql_insert);
if($res){
echo "哇哦,居然成功了";
header("refresh:5;url=./618.php"); //几秒后刷新并且跳转值某页面
echo "<br/>";
print('正在处理,请稍等...5秒后自动跳转至首页。');
}else{
echo "切";
}
?>
修改数据
- 方法一:
//updatework.php
//获取到id,根据id查询出某人的信息并显示到文本框中,从而进行修改
<html>
<head>
<meta charset="utf-8"> <!-- 头部设置utf-8 -->
<title>修改员工信息</title>
<style >
.box{margin:40;}
</style>
</head>
<?php
header('Content-Type: text/html; charset=utf-8');
$connect= new mysqli ('localhost','root','root','test');
$id = $_GET['id'];
$sql = "select id,name,FROM_UNIXTIME(birthday,'%Y-%m-%d') as birthday ,department,FROM_UNIXTIME(entry_time,'%Y-%m-%d') as entry_time from workinfo where id=$id ";
$res=$connect->query($sql);
$arr=$res->fetch_assoc();
?>
<body>
<div class="box" align="center">
<form action="update.php" method="post">
<table>
<tr>
<h2>修改员工信息</h2>
</tr>
<tr>
<th>编号:</th>
<th><input type="text" name="id" value="<?php echo $arr['id']?>"></th>
</tr>
<tr>
<th>姓名:</th>
<th><input type="text" name="name" value="<?php echo $arr['name']?>"></th>
</tr>
<tr>
<th>生日:</th>
<th><input type="text" name="birthday" value="<?php echo $arr['birthday']?>"></th>
</tr>
<tr>
<th>部门:</th>
<th><input type="text" name="department" value="<?php echo $arr['department']?>"></th>
</tr>
<tr>
<th>入职时间:</th>
<th><input type="text" name="entry_time" value="<?php echo $arr['entry_time'] ?>"></th>
</tr>
<tr>
</tr>
<tr>
<td><button type="submit">提交修改</button></td>
<td><button type="reset">重新编辑</button></td>
</tr>
</table>
<br>
</div>
</body>
</html>
?>
//update.php
<?php
header('Content-Type: text/html; charset=utf-8');
$connect= new mysqli ('localhost','root','root','test');
if(!empty($_POST)){
$id = $_POST["id"];
$name = $_POST["name"];
$department = $_POST["department"];
$birthday = strtotime($_POST['birthday']);
$entry_time = strtotime($_POST['entry_time']);
$sql = "update workinfo set name='$name',department='$department',birthday='$birthday',entry_time='$entry_time' where id='$id'";
$res = $connect->query($sql);
if($res){
echo "修改成功,可到数据库中查看";
header("refresh:5;url=./618.php");
echo "<br/>";
print('正在处理,请稍等...5秒后自动跳转至首页。');
}else{
echo "果然,修改失败,气死";
}
}else{
echo "错误!";
}
?>
- 方法二:
//public_function.php
<?php
header('Content-Type: text/html; charset=utf-8');
//连接数据库
function dbInit(){
$connect = new mysqli('localhost','root','root','test');
if($con === false){
echo 'false';
}else{
echo 'success';
}
$connect->query("set names 'utf8'");
$connect->query("use test");
return $connect;
}
//执行sql语句
function new_query($con,$sql){
$res=$con->query($sql);
if($res){
return $res;
}else{
echo 'echo'.$sql;
}
}
//处理单条数据的方法
function fetchOne($con,$sql){
$res = new_query($con,$sql);
if($res){
$row=mysqli_fetch_array($res,MYSQLI_ASSOC);
return $row;
}else{
return false;
}
}
?>
//doupdate.php
<html>
<head>
<meta charset="utf-8"> <!-- 头部设置utf-8 -->
<title>修改员工信息</title>
<style >
.box{margin:40;}
</style>
</head>
<body>
<div class="box" align="center">
<form action="update2.php?id=<?php echo $res['id']?>" method="post">
<table>
<tr>
<h2>修改员工信息</h2>
</tr>
<tr>
</tr>
<tr>
<th>姓名:</th>
<th><input type="text" name="name" value="<?php echo $res['name']?>"></th>
</tr>
<tr>
<th>生日:</th>
<th><input type="text" name="birthday" value="<?php echo $res['birthday']?>"></th>
</tr>
<tr>
<th>部门:</th>
<th><input type="text" name="department" value="<?php echo $res['department']?>"></th>
</tr>
<tr>
<th>入职时间:</th>
<th><input type="text" name="entry_time" value="<?php echo $res['entry_time'] ?>"></th>
</tr>
<tr>
</tr>
<tr>
<td><button type="submit">提交修改</button></td>
<td><button type="reset">重新编辑</button></td>
</tr>
</table>
<br>
</div>
</body>
</html>
//update2.php
<?php
header('Content-Type: text/html; charset=utf-8');
require './publicfunction.php';
$conn = dbInit();
$new_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
//判断是否有post提交进来
if(!empty($_POST)){ //有表单提交的时候,进行编辑员工信息的处理
$name = $_POST["name"];
$department = $_POST["department"];
$birthday = strtotime($_POST['birthday']);
$entry_time = strtotime($_POST['entry_time']);
$sql_update = "update workinfo set name='$name',department='$department',birthday='$birthday',entry_time='$entry_time' where id='$new_id'";
$result=new_query($new_connect,$sql_update);
if($result){
echo "修改成功,hhhh!";
header("refresh:5;url=./618.php");
echo "<br/>";
print('正在处理,请稍等...5秒后自动跳转至首页。');
}else{
echo "失败!";
}
}else{ //没有表单提交的时候,查询当前编辑的员工信息
$sql = " select id,name,FROM_UNIXTIME(birthday,'%Y-%m-%d') as birthday ,department,FROM_UNIXTIME(entry_time,'%Y-%m-%d') as entry_time from where id='$new_id'";
//或者为
// $sql = " select id,name,FROM_UNIXTIME(birthday,'%Y-%m-%d') as birthday ,department,FROM_UNIXTIME(entry_time,'%Y-%m-%d') as entry_time from where id={$_GET['id']}";
$res = fetchOne($conn,$sql);
require './doupdate.php';
}
?>
方法一和方法二的区别是:
(1) 方法二中封装了连接数据库,执行SQL语句以及处理单条数据的方法;方法一是每次连接和单独处理
(2)方法二中form表单提交页面,如果action=" " 即默认为提交至本页面,不需要传id,但若action到其他页面则需要传id(但此时id并不是以POST方式传值过去的),因为修改语句需要根据id来修改;若希望id以POST方式传值过处理修改的页面,则需要在表单中再加一列id,和name,department等一起以POST传值过去,例如方法一
删除数据
//deletework.php
//获得id,利用sql语句进行删除
<?php
$header('Content-Type: text/html; charset=utf-8');
$connect= new mysqli('localhost','root','root','test');
$id = $_GET['id'];
$sql_delete = " delete from workinfo where id = $id ";
$res=$connect->query($sql_delete);
if($res){
echo "删除成功!";
header("refresh:5;url=./618.php");
echo "<br/>";
print('正在处理,请稍等...5秒后自动跳转至首页。');
}else{
echo "删除失败";
}
?>
关于strtostime()函数
在PHP中我们使用strotime()函数可以将任何英文文本的日期和时间解析为UNIX时间戳
语法格式:strtotime(time,now);
time:被解析的字符串,格式根据 GNU » 日期输入格式 的语法。strtotime的第一个参数可以是我们常见的英文时间格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以参数now为基准的时间描述,比如“+1 day”等等。
now:用来计算返回值的时间戳。 该参数默认值是当前时间time(),也可以设置为其他时间的时间戳
返回值: 成功则返回间戳,否则返回 FALSE 。在 PHP 5.1.0 之前本函数在失败时返回 -1,后面版本返回false.
header(“refresh:5;url=./618.php”);
print(‘正在处理,请稍等…5秒后自动跳转至首页。’);
//几秒后刷新至某页面
关于mysqli_fetch_assoc()和mysqli_fetch_array()
(1)这二者是在执行SQL查询语句后,返回结果集的
(而我因为刚开始不清楚就乱用,在update语句后,执行了public_function.php中的fetchOne()导致报错,其实只需执行new_query()就可以,因为只需要执行sql语句而不用返回结果集,其次参数类型不匹配。)
(2)fetch_array()如果单独作为while的条件,则能够取出结果集中的所有结果。如果单独作用于结果集,只能从结果集中取出数据库结果集一行。
mysqli_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有。由第二个参数决定(MYSQLI_ASSOC或MYSQLI_NUM)该函数返回的字段名是区分大小写的。
(3)mysqli_fetch_assoc() 函数从结果集中取得一行作为关联数组。该函数返回的字段名是区分大小写的。
雪碧可乐_2020/6/19_20:21
雪碧可乐_2020/6/20_9:10