有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享
路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。
今天给大家接着上篇文章添加图书添加功能,来实现图书的修改功能。
1 从列表页面开始
点解列表页面的编辑按钮,可以跳转到修改页面。
此时的编辑代码配置如下,显然我们要创建一个editBook.php的编辑图书的跳转php控制文件。
2 编写editBook.php文件
下面代码是editBook.php文件的内容:
<?php
$dbName = 'db_test';
$link = mysqli_connect('localhost','root','passwd',$dbName) or die("连接数据库服务器失败!".mysqli_error());
$result = mysqli_query($link,"set names utf8");
$id = $_GET['id'];
$sql = 'select * from books where id ='.$id;
$result = mysqli_query($link,$sql);
$data = mysqli_fetch_assoc($result);
include_once('edit.html');
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
从代码中可以看出,我们配置了数据库连接,设置编码格式为utf-8,并根据id查询出指定的图书返回到edit.html页面,所以,此时我们要创建edit.html文件来展示editBook.php查出来的图书信息。
3 编写edit.html展示页面
edit.html脚本如下:
<!DOCTYPE html>
<html lang="en" class="is-centered is-bold">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>添加图书</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/moment.js/2.22.0/moment-with-locales.js"></script>
<script src="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-sm-offset-2 col-sm-8">
<div class="panel panel-default">
<div class="panel-heading">
修改图书信息
</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="post" action="updateBook.php">
<input type="hidden" name="id" value="<?php echo $data['id'] ?>">
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="name" class="col-md-3 control-label">书名</label>
<div class="col-md-9">
<input type="text" class="form-control" name="name" id="name"
value="<?php echo $data['name'] ?>">
</div>
</div>
<div class="form-group">
<label for="name" class="col-md-3 control-label">类别</label>
<div class="col-md-9">
<select name="category" class="form-control">
<option value="PHP类" <?php if($data['category']== "PHP类")
echo 'selected'; ?> > PHP类</option>
<option value="JAVA类" <?php if($data['category']== "JAVA类")
echo 'selected'; ?> >JAVA类</option>
<option value="VUE类" <?php if($data['category']== "VUE类")
echo 'selected'; ?> >VUE类</option>
<option value="CPP类" <?php if($data['category']== "CPP类")
echo 'selected'; ?> >CPP类</option>
</select>
</div>
</div>
<div class="form-group">
<label for="price" class="col-md-3 control-label">价格</label>
<div class="col-md-9">
<input type="text" class="form-control" name="price" id="price"
value="<?php echo $data['price'] ?>">
</div>
</div>
<div class="form-group">
<label for="price" class="col-md-3 control-label" for="publish_time">出版时间</label>
<div class="col-md-9">
<div class="input-group date" id="publish_time">
<input type="text" class="form-control" name="publish_time"
value="<?php echo $data['publish_time'] ?>"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<div class="col-md-6 col-md-offset-6">
<button type="submit" class="btn btn-primary btn-lg">
<i class="fa fa-disk-o"></i>
确认修改
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
$('#publish_time').datetimepicker({
format: 'YYYY-MM-DD',
locale: moment.locale('zh-cn'),
defaultDate: new Date()
});
$(function () {
$('#publish_time').datetimepicker({
format: 'YYYY-MM-DD HH:mm',
locale: moment.locale('zh-tw')
});
});
</script>
</body>
</html>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
注意:请仔细研究下上述edit.html页面内部的php代码,数据是怎么接收的。
界面如下图:
这仍然是一个表单,回显了id为2的图书信息,思考下怎么回显的。既然是个表单,当点击确认修改后,肯定要来个php后端逻辑来接收修改的数据,此时仔细看edit.html文件就能看到: action="updateBook.php",所以我们要创建updateBook.php文件。
4 编写updateBook.php
updateBook.php文件内如如下:
<?php
$id = $_POST['id'];
$name = $_POST['name'];
$category = $_POST['category'];
$price = $_POST['price'];
$publish_time = $_POST['publish_time'];
$dbName = 'db_test';
$link = mysqli_connect('localhost','root','passwd',$dbName) or die("连接数据库服务器失败!".mysqli_error());
mysqli_query($link,"set names utf8");
$update_sql = 'update books set name=?,category=?,price=?,publish_time=? where id ='.$id;
$stmt = mysqli_prepare($link,$update_sql);
if(!$stmt){
die('mysqli error:'.mysqli_error($link));
}
//实现参数的绑定
mysqli_stmt_bind_param($stmt, 'ssds',$name,$category,$price,$publish_time);
//进行执行sql
mysqli_stmt_execute($stmt);
//检测执行行数,返回0表示没有记录更新,返回n行数说明更新了n条数据 -1代表异常。
if(mysqli_stmt_affected_rows($stmt)){
echo "<script>alert('修改成功!');window.location.href='index.php';</script>";
}else{
echo "<script>alert('修改失败!');</script>";
}
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
修改后返回到index.php页面:
搞定修改,爽歪歪吧!!!
下篇 图书信息删除操作