有胆量你就来跟着路老师卷起来! -- 纯干货,技术知识分享
路老师给大家分享PHP语言的知识了,旨在想让大家入门PHP,并深入了解PHP语言。
今天给大家接着上篇文章编写图书添加功能。
1 添加页面
创建add.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="addBook.php">
<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="">
</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类" selected>PHP类</option>
<option value="JAVA类">JAVA类</option>
<option value="VUE类">VUE类</option>
<option value="CPP类">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="">
</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" />
<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>
2 编写php后端的实现逻辑
创建文件addBook.php文件。(注释很详细)
<?php
//指定数据库为db_test
$dbName = 'db_test';
//数据库服务器连接
$link = mysqli_connect('localhost','root','passwd');
//如果数据库db_test连接成功,则进入数据插入功能逻辑
if(mysqli_select_db($link,$dbName)) {
//数据库编码格式设置成utf-8
$result = mysqli_query($link,"set names utf8");
//使用安全的prepareStatement语法实现sql的插入
$stmt = mysqli_prepare($link,"insert into books (name,category,price,publish_time)values(?,?,?,?)");
if(!$stmt){
die('mysqli error:'.mysqli_error($link));
}
$name=$_POST['name'];
$category=$_POST['category'];
$price=$_POST['price'];
$publish_time=$_POST['publish_time'];
//实现参数的绑定
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>";
}
} else{
echo $dbName."未连接!";
}
?>
对上述代码进行补充说明:
mysqli_stmt_bind_param($stmt, 'ssds',$name,$category,$price,$publish_time);
第一个参数$stmt 是statement标识符.
第二个参数'ssds'代表后面的四个变量分别代表的类型是string ,string ,double,string。
这个类型在PHP中总共包含四类:
字符种类 | 代表的数据类型 |
i | integer |
d | double |
s | string |
b | blob |
3 展示效果
注意:一定要看好浏览器输入的方式:http://域名/add.html
输入图书信息,和出版日期,点击提交按钮,进入图书列表页面:
原来是这样的:
添加后结果如下:
下篇 图书信息管理编辑