PHP 结合 Bootstrap 实现学生列表以及添加学生功能实现(继上篇登录及注册功能之后)
本人是一位学生,正在学习当中,可能BUG众多,请见谅并指正,谢谢!!!
学生列表实现
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生信息</title>
<link rel="stylesheet" type="text/css" href="css/Bootstrap.css">
</head>
<body>
<div class="container mt-5">
<h1 class="display-4 text-center">学生信息管理中心</h1>
<div class="row mt-3">
<a class="btn btn-info col-sm-2" style="margin-right: 88px; margin-left: 15px;" href="add_student.php">添加学生</a>
<input type="text" class="form-control col-sm-6 ml-5" placeholder="请输入关键词">
<button type="submit" class="btn btn-info col-sm-2 ml-4">点击搜索</button>
</div>
<table class="table table-hover table-bordered mt-3">
<thead class="thead-inverse">
<tr>
<th class="text-center align-middle">学号</th>
<th class="text-center align-middle">学院/系</th>
<th class="text-center align-middle">班级</th>
<th class="text-center align-middle">姓名</th>
<th class="text-center align-middle">性别</th>
<th class="text-center align-middle">年龄</th>
<th class="text-center align-middle">照片</th>
<th class="text-center align-middle">操作</th>
</tr>
</thead>
<tbody class="text-center">
<?php while ($student = mysqli_fetch_assoc($query)): ?>
<tr>
<td class="align-middle"><?php echo $student['num']; ?></td>
<td class="align-middle"><?php echo $student['system']; ?></td>
<td class="align-middle"><?php echo $student['class']; ?></td>
<td class="align-middle"><?php echo $student['name']; ?></td>
<td class="align-middle"><?php echo $student['gender'] === 1 ? '♂' : '♀'; ?></td>
<td class="align-middle"><?php echo date('Y') - substr($student['birthday'], 0, 4); ?></td>
<td class="align-middle"><img src="<?php echo $student['photo']; ?>" width="100" height="70"></td>
<td class="align-middle"><a class="btn btn-info mr-2" href="edit.php?num=<?php echo $student['num']; ?>">编辑</a><a class="btn btn-danger ml-2" href="delete.php?num=<?php echo $student['num']; ?>">删除</a></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
</div>
</body>
</html>
PHP:
// 这里:
// 第一个参数:本地网络地址
// 第二个参数:数据库账号
// 第三个参数:数据库密码
// 第四个参数:数据库名称
$connection = mysqli_connect('localhost', 'root', '密码', 'students_info_system'); if (!$connection) {
exit('<h1>连接数据库失败</h1>');
} $query = mysqli_query($connection, 'select * from students'); if (!$query) {
exit('<h1>学生数据查询失败</h1>');
} $administrator_query = mysqli_query($connection, 'select * from students');
添加学生实现
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加学生</title>
<link rel="stylesheet" type="text/css" href="css/Bootstrap.css">
</head>
<body>
<div class="container mt-3">
<h1 class="display-4 text-center">添加学生</h1>
<?php if (isset($error_msg)): ?>
<div class="alert alert-danger"><?php echo $error_msg; ?></div>
<?php endif ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" autocomplete="off">
<div class="form-group">
<input type="number" name="num" class="form-control" placeholder="学号" value="<?php echo isset($_POST['num']) ? $_POST['num']: ''; ?>">
</div>
<div class="form-group">
<select class="form-control" name="system">
<option>请选择学院/系</option>
<option>电气工程学院</option>
<option>信息工程与艺术学院</option>
<option>国际教育学院</option>
<option>水利水电工程学院</option>
<option>测绘与市政工程学院</option>
<option>马克思主义学院</option>
<option>建筑工程学院</option>
<option>经济与管理学院</option>
</select>
</div>
<div class="form-group">
<input type="text" name="class" class="form-control" placeholder="班级" value="<?php echo isset($_POST['class']) ? $_POST['class'] : ''; ?>">
</div>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="姓名" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>">
</div>
<div class="form-group">
<select class="form-control" name="gender">
<option value="-1">请选择性别</option>
<option <?php echo isset($_POST['gender']) && $_POST['gender'] === '1' ? 'selected' : ''; ?> value="1">男</option>
<option <?php echo isset($_POST['gender']) && $_POST['gender'] === '0' ? 'selected' : ''; ?> value="0">女</option>
</select>
</div>
<div class="form-group">
<label for="birthday">出生日期</label>
<input type="date" name="birthday" class="form-control" id="birthday" value="<?php echo isset($_POST['birthday']) ? $_POST['birthday'] : ''; ?>">
</div>
<div class="form-group">
<label for="photo">照片</label>
<input type="file" name="photo" class="form-control">
</div>
<button class="btn btn-info btn-block">确认添加</button>
</form>
</div>
</body>
</html>
PHP:
function add_student () {
$connection = mysqli_connect('localhost', 'root', '密码', 'students_info_system');
if (!$connection) {
$GLOBALS['error_msg'] = '连接数据库失败';
return;
}
$query = mysqli_query($connection, 'select * from students');
if (!$query) {
$GLOBALS['error_msg'] = '数据查询失败';
return;
}
// 查询已有学生
while ($student = mysqli_fetch_assoc($query)) {
// var_dump($student);
$students_num[] = $student['num'];
}
// var_dump($_FILES['photo']);
// var_dump($_POST['gender']);
if (empty($_POST['num'])) {
$GLOBALS['error_msg'] = '请输入学号';
return;
}
// 判断该学号是否已经被添加(即列表中已存在该学生)=========
if (in_array($_POST['num'], $students_num)) {
$GLOBALS['error_msg'] = '该学生已存在';
return;
}
if (empty($_POST['system']) || $_POST['system'] === '请选择学院/系') {
$GLOBALS['error_msg'] = '请选择学院/系';
return;
}
if (empty($_POST['class'])) {
$GLOBALS['error_msg'] = '请输入班级';
return;
}
if (empty($_POST['name'])) {
$GLOBALS['error_msg'] = '请输入姓名';
return;
}
if (!(isset($_POST['gender']) && $_POST['gender'] !== '-1')) {
$GLOBALS['error_msg'] = '请选择性别';
return;
}
if (empty($_POST['birthday'])) {
$GLOBALS['error_msg'] = '请输入出生日期';
return;
}
// 以下处理文件域=======================================================
if (empty($_FILES['photo'])) {
$GLOBALS['error_msg'] = '请上传照片';
return;
}
if ($_FILES['photo']['error'] !== UPLOAD_ERR_OK) {
$GLOBALS['error_msg'] = '上传照片失败';
return;
}
// 验证上传文件的类型(只允许图片)
if (strpos($_FILES['photo']['type'], 'image/') !== 0) {
$GLOBALS['error_msg'] = '这不是支持的文件格式类型,请重新上传';
return;
}
// 文件上传到了服务端开辟的一个临时地址,需要转移到本地
$image_target = 'images/' . $_FILES['photo']['name'];
if (!move_uploaded_file($_FILES['photo']['tmp_name'], $image_target)) {
$GLOBALS['error_msg'] = '图片上传失败';
return;
}
// 接收学生信息
$num = (string)$_POST['num'];
$system = (string)$_POST['system'];
$class = (string)$_POST['class'];
$name = (string)$_POST['name'];
$gender = (int)$_POST['gender'];
$birthday = (string)$_POST['birthday'];
$photo = (string)$image_target;
// 将数据存放到数据库
$insert_query = mysqli_query($connection, "insert into students values ('{$num}', '{$system}', '{$class}', '{$name}', {$gender}, '{$birthday}', '{$photo}')");
if (!$insert_query) {
$GLOBALS['error_msg'] = '数据查询失败';
return;
}
$affected_rows = mysqli_affected_rows($connection);
if ($affected_rows !== 1) {
$GLOBALS['error_msg'] = '添加失败';
return;
}
// 延迟2秒
time_sleep_until(time() + 2);
// 跳回到学生信息页面
header("Location: student_info.php");
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
add_student();
}
PHP 结合 Bootstrap 实现学生列表以及添加学生功能实现(继上篇登录及注册功能之后)的更多相关文章
- Bootstrap框架--DataTables列表示例--添加判断
一.参考代码 <%@ include file="./include/header.jsp"%> <!-- jquery.dataTables.css --> ...
- Bootstrap历练实例:向列表组添加内容
向列表组添加自定义内容 我们可以向上面已添加链接的列表组添加任意的 HTML 内容.下面的实例演示了这点: <!DOCTYPE html><html><head>& ...
- Bootstrap历练实例:向列表组添加链接
向列表组添加链接 通过使用锚标签代替列表项,我们可以向列表组添加链接.我们需要使用 <div> 代替 <ul> 元素.下面的实例演示了这点: <!DOCTYPE html ...
- PHP 结合 Boostrap 结合 js 实现学生列表删除编辑以及搜索功能(完结)
这个自己的小项目要先告一段落了.可能还有许多bug.请见谅 删除学生功能 PHP: // 这里是通过前端代码HTML中的 url 传过来的,用 $_GET 来获取(相关HTML代码可以看一下到主页看一 ...
- Vue学习之路第十六篇:车型列表的添加、删除与检索项目
又到了大家最喜欢的项目练习阶段,学以致用,今天我们要用前几篇的学习内容实现列表的添加与删除. 学前准备: ①:JavaScript中的splice(index,i)方法:从已知数组的index下标开始 ...
- MVC学生管理系统-阶段II(添加学生信息)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示 请看上一篇文章 本文是对阶段 ...
- Vue和Element基础使用,综合案例学生列表实现
知识点梳理 课堂讲义 1.Vue 快速入门 1.1.Vue的介绍 Vue是一套构建用户界面的渐进式前端框架. 只关注视图层,并且非常容易学习,还可以很方便的与其它库或已有项目整合. 通过尽可能简单的A ...
- 数据库中老师学生家长表添加自动同意好友自动(AgreeAddingFriend ),默认为True
数据库中老师学生家长表添加自动同意好友自动(AgreeAddingFriend ),默认为True alter table Sys_User add AgreeAddingFriend bit alt ...
- bootstrap制作搜索框及添加回车搜索事件
下面是开发中用bootstrap制作的一个搜索框,以及给搜索框添加回车搜索事件的一个小案例. bootstrap制作搜索框及添加回车搜索事件 下面是功能实现的代码: <!DOCTYPE html ...
随机推荐
- 《JAVA程序设计》_第十周学习总结
一.学习内容 12.1进程与进程 程序是一段静态的代码,进程是程序的一次动态执行过程,这个过程也是进程本身从产生.发展至消亡的过程. 线程不是进程,是比进程更小的执行单位.但与进程不同的是,线程的中断 ...
- hadoop平台上HDFS和MAPREDUCE的功能、工作原理和工作过程
作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapReduce ...
- Java-Maven(十二):idea多项目:common module进行compiler和install正常,运行domain-perf module提示:Could not resolve dependencies for project
前提: product项目下有三个module,分别是: driver module domain-perf module common module 问题: driver 和 domain-perf ...
- c# 通过json.net中的JsonConverter进行自定义序列化与反序列化
https://www.cnblogs.com/yijiayi/p/10051284.html 相信大家在工作中会经常遇见对json进行序列化与反序列化吧,但通常的序列化与反序列化中的json结构与c ...
- JS高级:面向对象的构造函数
1 创建对象的方式 1.1 字面量的方式创建对象 var p1 = { name: '张三', run: function () { console.log(this.name + '跑'); } } ...
- Pycharm连接远程服务器并进行代码上传+远程调试
前提:需要有一个远程服务器,知道他的ip.port.user.password 一.连接远程服务器 进入配置页面 Pycharm菜单栏,如下图所示,依次点击 Tools -> Deploymen ...
- curl 转 wget
curl 转 wget // sed -e 's@-H @--header=@g;s@^curl @wget @g;s@--compressed$@@g' $crf var curlStr = `cu ...
- Qt编写图片及视频TCP/UDP网络传输
一.前言 很多年前就做过类似的项目,无非就是将本地的图片上传到服务器,就这么简单,其实用http的post上传比较简单容易,无需自定义协议,直接设置好二进制数据即可,而采用TCP或者UDP通信的话,必 ...
- k8s记录-yum本地仓库部署
#1.安装插件yum install -y yum-plugin-downloadonly createrepo rsync #2.创建仓库目录mkdir -p /mirrors/centos#3.下 ...
- Jenkins - 插件管理
1 - Jenkins插件 Jenkins通过插件来增强功能,可以集成不同的构建工具.云平台.分析和发布工具等,从而满足不同组织或用户的需求. Jenkins 提供了不同的的方法来安装插件(需要不同级 ...