今天和大家分享一个小案例,也就是平时比较常见的增删改查,本人现在从事教育行业,发现有很多小伙伴不太明白网页同步操作和异步操作的区别,你可以简单理解为,页面是否跳转,关于异步操作在我的这编文章,https://blog.csdn.net/Only_ruiwen/article/details/79188555 中有介绍到,我们今天来看下同步操作是怎么回事?
我用的编辑器是vscode一个比较轻量级的开发软件,但是要下载插件,才会有代码提示,你可以自行百度一下,php插件。。。
目录结构:
数据库结构
我们来看下lib/mysql.func.php这里主要存放着增删改查的功能函数。
<?php
/**
* 连接数据库
* $link 资源
*
*/
function connect(){
$link=mysqli_connect("localhost","root","","goods") or die("数据连接失败");
mysqli_query($link,"set names utf8") or die("设置字符集错了");
return $link;
}
$arr = $_POST;
connect();
// 插入数据库 insert into
function insert ($table,$array){
$conn=connect();
$keys=join(",",array_keys($array));
$vals="'".join("','",array_values($array))."'";
$sql="INSERT INTO {$table} ({$keys})VALUES ({$vals})";
if (mysqli_query($conn,$sql)) {
return null;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
return $sql;
}
}
// 查找数据库 获取所有的数据
function fetch_one($sql,$result_type=MYSQLI_ASSOC){
$conn = connect();
$result = mysqli_query($conn,$sql);
$rows = array();
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
}
return $rows;
}
//删除数据库数据 删除数据 delete from book where id=1;
function delete($table,$where){
$conn = connect();
$where = $where==null?null:" where id=".$where;
$sql = "delete from {$table}{$where}";
$res = mysqli_query($conn,$sql);
if($res){
return true;
}else{
return false;
}
}
//更新数据库数据 更新数据 update book set book_name
function update($table,$array){
$conn = connect();
$str = "";
foreach($array as $key=>$val){
if($str==""){
$sep = "";
}else{
$sep = ",";
}
$str.=$sep.$key."='".$val."'";
}
$sql = "update {$table} set {$str} where id={$array['id']}";
$res = mysqli_query($conn,$sql);
if($res){
return true;
}else{
return false;
}
}
展示数据页面 showAll.php
<?php
require("./lib/mysql.func.php");
$res=fetch_one("select * from book");
// foreach($res as $key=>$value){
// // echo $value["id"];
// }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.11.0.js" ></script>
<link rel="stylesheet" href="css/bootstrap.css" />
</head>
<body>
<div class="container">
<table class="table table-bordered table-hover text-center">
<tr>
<td>图书编号</td>
<td>图书姓名</td>
<td>图书价格</td>
<td>出版时间</td>
<td>图书类型</td>
<td>操作</td>
</tr>
<?php
foreach($res as $key=>$value){
?>
<tr>
<td><?php echo $value["id"];?></td>
<td><?php echo $value["book_name"];?></td>
<td><?php echo $value["book_price"];?></td>
<td><?php echo $value["book_time"];?></td>
<td><?php echo $value["book_type"];?></td>
<td>
<button class="btn btn-primary update" type="button" data-id=<?php echo $value["id"]?>>更新</button>
<button class="btn btn-danger del" type="button" data-id=<?php echo $value["id"]?>>删除</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
<script>
$(function(){
$(".update").click(function(){
var id = $(this).data("id")
window.location = "form.php?id="+id
})
$(".del").click(function(){
var id = $(this).data("id")
window.location ="./action/doAction.php?action=del&&id="+id;
})
})
</script>
</body>
</html>
修改页面 form.php
<?php
require("./lib/mysql.func.php");
$id=$_REQUEST["id"];
$res=fetch_one("select * from book where id='$id'");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.11.0.js" ></script>
<link rel="stylesheet" href="css/bootstrap.css" />
</head>
<body>
<div class="container">
<form method="post" action="./action/doAction.php?action=update">
<div class="form-group">
图书编号:<input type="text" name="id" id="book_name" class="form-control" value='<?php echo $res[0]['id'];?>' readonly='true'>
</div>
<div class="form-group">
书名称:<input type="text" name="book_name" id="book_name" class="form-control" value='<?php echo $res[0]["book_name"];?>'>
</div>
<div class="form-group">
价格:<input type="text" name="book_price" id="book_price" class="form-control" value='<?php echo $res[0]["book_price"];?>'>
</div>
<div class="form-group">
出版时间:<input type="text" name="book_time" id="book_time" class="form-control" value='<?php echo $res[0]["book_time"];?>'>
</div>
<div class="form-group">
所属类别:<input type="text" name="book_type" id="book_type" class="form-control" value='<?php echo $res[0]["book_type"];?>'>
</div>
<button class="login btn btn-danger" type="submit">提交</button>
</form>
</div>
<script>
</script>
</body>
</html>
action下的doAction.php
<?php
require("../lib/mysql.func.php");
$action = $_GET["action"];
if($action=="update"){
$arr = $_POST;
$res = update("book",$arr);
if($res){
echo "<span>修改成功</span>";
echo "<a href='../showAll.php'>返回列表</a>";
}else{
echo "<span>修改失败</span>";
echo "<a href='../form.php'>返回修改</a>";
}
}else if($action=="del"){
$id=$_GET["id"];
$res = delete("book",$id);
if($res){
echo "删除成功";
echo "<a href='../showAll.php'>返回列表</a>";
}else{
echo "删除失败";
echo "<a href='../showAll.php'>返回列表</a>";
}
}