PHP 结合 Bootstrap 实现学生列表以及添加学生功能实现(继上篇登录及注册功能之后)
程序员文章站
2022-03-14 10:00:33
本人是一位学生,正在学习当中,可能BUG众多,请见谅并指正,谢谢!!! 学生列表实现 HTML: PHP: 添加学生实现 HTML: PHP: ......
本人是一位学生,正在学习当中,可能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来写记数器(详细介绍)
下一篇: 杏林同学录(七)