PHP 結合 Boostrap 結合 js 實現學生列表刪除編輯以及搜索功能(完結)

這個自己的小項目要先告一段落了。可能還有許多bug。請見諒

?

?

刪除學生功能

PHP:

// 這里是通過前端代碼HTML中的 url 傳過來的,用 $_GET 來獲取(相關HTML代碼可以看一下到主頁看一下前幾條博客)
if
(empty($_GET['num'])) exit('<h1>找不到您要刪除的學生的學號</h1>');$num = $_GET['num'];$connection = mysqli_connect('localhost', 'root', '密碼', 'students_info_system');if (!$connection) exit('<h1>連接數據庫失敗</h1>');$query = mysqli_query($connection, "delete from students where num = {$num}");if (!$query) exit('<h1>該學生信息查詢失敗</h1>');// 注意:這里傳入的是連接對象 $affected_rows = mysqli_affected_rows($connection);if ($affected_rows !== 1) exit('<h1>刪除失敗</h1>');header('Location: student_info.php');

編輯學生功能(整體上和添加學生功能差不到,稍微有些許變化)

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-5 text-center">編輯學生</h1><?php if (isset($error_msg)): ?><div class="alert alert-danger"><?php echo $error_msg; ?></div><?php endif ?><div class="row mt-3"><img src="<?php echo $current_student['photo']; ?>" alt="<?php echo $current_student['name']; ?>" width="100" height="488" class="col-sm-6"><form action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $current_num; ?>" method="post" enctype="multipart/form-data" autocomplete="off" class="col-sm-6"><div class="form-group"><input type="number" name="num" class="form-control" placeholder="學號" value="<?php echo isset($_POST['num']) ? $_POST['num'] : $current_student['num']; ?>"></div><div class="form-group"><select class="form-control" name="system"><option>請選擇學院/系</option><option <?php echo $current_student['system'] === '電氣工程學院' ? 'selected' : ''; ?>>電氣工程學院</option><option <?php echo $current_student['system'] === '信息工程與藝術學院' ? 'selected' : ''; ?>>信息工程與藝術學院</option><option <?php echo $current_student['system'] === '國際教育學院' ? 'selected' : ''; ?>>國際教育學院</option><option <?php echo $current_student['system'] === '水利水電工程學院' ? 'selected' : ''; ?>>水利水電工程學院</option><option <?php echo $current_student['system'] === '測繪與市政工程學院' ? 'selected' : ''; ?>>測繪與市政工程學院</option><option <?php echo $current_student['system'] === '馬克思主義學院' ? 'selected' : ''; ?>>馬克思主義學院</option><option <?php echo $current_student['system'] === '建筑工程學院' ? 'selected' : ''; ?>>建筑工程學院</option><option <?php echo $current_student['system'] === '經濟與管理學院' ? 'selected' : ''; ?>>經濟與管理學院</option></select></div><div class="form-group"><input type="text" name="class" class="form-control" placeholder="班級" value="<?php echo isset($_POST['class']) ? $_POST['class'] : $current_student['class']; ?>"></div><div class="form-group"><input type="text" name="name" class="form-control" placeholder="姓名" value="<?php echo isset($_POST['name']) ? $_POST['name'] : $current_student['name']; ?>"></div><div class="form-group"><select class="form-control" name="gender"><option value="-1">請選擇性別</option><option <?php echo $current_student['gender'] === '1' ? 'selected' : ''; ?> value="1">男</option><option <?php echo $current_student['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'] : $current_student['birthday']; ?>"></div><div class="form-group"><label for="photo">照片</label><input type="file" name="photo" class="form-control"></div><button type="submit" class="btn btn-info btn-block">確認修改</button></form></div></div>
</body>
</html>

PHP:

if (empty($_GET['id'])) exit('<h1>必須指定相應的學號</h1>');$current_num = $_GET['id'];$connection = mysqli_connect('localhost', 'root', '密碼', 'students_info_system');if (!$connection) exit('<h1>連接數據庫失敗</h1>');$query = mysqli_query($connection, "select * from students where num = {$current_num} limit 1");if (!$query) exit('<h1>找不到您要編輯的學生信息</h1>');$current_student = mysqli_fetch_assoc($query);// var_dump($current_student);function edit_student() {// var_dump('進來了');global $connection;global $current_num;    // 當前學生學號global $current_student;$extra_students_query = mysqli_query($connection, "select * from students where num != {$current_num}");if (!$extra_students_query) {exit('<h1>其余學生數據查詢失敗</h1>');// return;
    }// 查詢除該學生以外的其他學生while ($student = mysqli_fetch_assoc($extra_students_query)) {// var_dump($student);$students_num[] = $student['num'];}// var_dump($students_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;}// 以下處理文件域=======================================================// 當有文件上傳時才驗證,沒有上傳則照片不變// $_FILES['photo'] = $current_student['photo'];// var_dump($_FILES['photo']);if ($_FILES['photo']['name'] !== '') {// var_dump($_FILES['photo']);// var_dump($_FILES['photo']);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;}// 接收更新過的學生照片$current_student['photo'] = (string)$image_target;} else {// var_dump($_FILES['photo']);// 如果照片沒有上傳則不進行驗證文件域,直接更新數據且不改變原來的照片$current_student['num'] = $_POST['num'];$current_student['system'] = $_POST['system'];$current_student['class'] = $_POST['class'];$current_student['name'] = $_POST['name'];$current_student['gender'] = $_POST['gender'];$current_student['birthday'] = $_POST['birthday'];}// var_dump($current_num);// 將數據修改存放到數據庫$update_query = mysqli_query($connection, "update students set `num` = '{$current_student['num']}', `system` = '{$current_student['system']}', `class` = '{$current_student['class']}', `name` = '{$current_student['name']}', `gender` = '{$current_student['gender']}', `birthday` = '{$current_student['birthday']}', `photo` = '{$current_student['photo']}' where `num` = {$current_num}");if (!$update_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') {edit_student();
}

搜索功能(用js)

// 關鍵詞搜索功能----立即函數
(function (element, search_key) {let table = document.getElementById('table-content'); // 獲取表格function in_array_item (item, array) {for (var i = 0; i < array.length; i++) {if (array[i].indexOf(item) != -1) {return true;}}return false;}function response () {let hiddenStudentsNumber = 0;                          // 獲取隱藏的學生個數(即表格隱藏行數)// 獲取要搜索的關鍵詞const search_content = document.getElementById(search_key).value;// console.log(search_content);// console.log(typeof(search_content));
let data = [];// 遍歷列表將數據存儲到一個數組中// 1.獲取表格行數for (let i = 0; i < table.children.length; i++) {// 2.獲取表格列數for (let j = 0; j < table.children[i].children.length; j++) {if (!data[i]) {// 在數組中創鍵每一行內容存放的數組,用于存放一行數據data[i] = new Array();}data[i][j] = table.children[i].children[j].innerHTML.toString();// 3.存放數據if (data[i][j] === '♂') {data[i][j] = '男';}if (data[i][j] === '♀') {data[i][j] = '女';}}// console.log(data[i]);if (search_content == '') {table.children[i].style.display = '';} else {if (in_array_item(search_content, data[i])) {table.children[i].style.display = '';} else {table.children[i].style.display = 'none';hiddenStudentsNumber += 1;}}}console.log(hiddenStudentsNumber);let str = "共有" + (table.children.length - hiddenStudentsNumber) + "名學生";document.getElementById('students_number').innerHTML = str;}const searchButton = document.getElementById(element);searchButton.addEventListener('click', function () {response();});document.addEventListener('keydown', function (event) {if (event.keyCode === 13) {response();}});let str = "共有" + table.children.length + "名學生";document.getElementById('students_number').innerHTML = str;
})('search', 'search-key');

同時在原有的學生信息頁面HTML添加:

<div class="row mt-3"><a class="btn btn-info col-sm-2" style="margin-right: 28px; margin-left: 15px;" href="add_student.php">添加學生</a>
      
        // 添加的<button class="btn btn-info align-middle" id="students_number"></button>
        
<input type="text" class="form-control col-sm-6 ml-2" autocomplete="on" placeholder="請輸入關鍵詞" value="" id="search-key"><button type="submit" class="btn btn-info col-sm-2 ml-2" id="search">點擊搜索</button></div>

轉載于:https://www.cnblogs.com/duxiu-fang/p/10899392.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448916.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448916.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448916.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ActiveMQ_Windows版本的安裝部署

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1, 保證電腦上安裝了jdk6以上版本的java&#xff0c;并配置了好環境變量 &#xff1b; 2, 官方下載地址&#xff1a;http://activemq.a…

Java 自定義異常(轉載)

1.異常的分類 1. 非運行時異常(Checked Exception) Java中凡是繼承自Exception但不是繼承自RuntimeException的類都是非運行時異常。 2. 運行時異常&#xff08;Runtime Exception/Unchecked Exception&#xff09; RuntimeException類直接繼承自Exception類&#xff0c;稱為運…

如何將markdown轉換為wxml

話說我要為技術博客寫一個小程序版&#xff0c;我的博客解決方案是 hexo github-page&#xff0c;格式當然是技術控們喜歡的 markdown 了 。但小程序使用的卻是獨有的模版語言 WXML。我總不能把之前的文章手動轉換成小程序的 wxml 格式吧&#xff0c;而網上也沒完善的轉換庫&a…

巧妙喝水打敗多種疾病

喝水&#xff0c;我們每天都會做的一件事&#xff0c;殊不知&#xff0c;喝水得當能打敗多種疾病問題! 方法/步驟 一、很多人都聽說過早晨喝一杯水對身體有好處&#xff0c;有人喝鹽水?有人喝蜂蜜水?還有人為了美白喝檸檬水?到底喝什么水最好呢?人體經過了一宿的代謝&…

git 幾個commit點合并成一個commit點

在用git做版本控制器的時候&#xff0c;經常會遇到以下情況&#xff1a; 1、在做1個功能的時候&#xff0c;你自己覺得代碼沒問題了&#xff0c;就本地commit&#xff0c;然后提交代碼&#xff0c;在gitlab上發起和并請求&#xff0c;老大看完之后&#xff0c;覺得你還有修改的…

JNDI 是什么

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 JNDI : 簡單理解&#xff0c;就是把固定的連接方式剝離出來&#xff0c;單獨寫在一個配置文件里。(下載.properties里面通過InputStream…

并發編程模型

并發編程模型 一.分類 按照線程通信機制可以分為共享內存模型和消息傳遞模型&#xff1a; 1.共享內存模型&#xff1a;線程之間共享程序的公共狀態&#xff0c;編程之間通過讀寫內存中的公共狀態來隱式進行通信。相互通信的進程共享某些數據結構或共享存儲區&#xff0c;進程通…

換工作,讓我里外不是人,到底錯在哪兒

看看時間&#xff0c;現在是凌晨4點多&#xff0c;窗外時不時一道閃電&#xff0c;雨也一陣一陣的&#xff0c;雷聲像逐漸遠離的喧囂的火車一樣。我不是睡眠困難者&#xff0c;但是&#xff0c;昨晚吃完晚飯之后&#xff0c;在衣服都未脫的情況下&#xff0c;暈暈乎乎的睡到現在…

Flink Kafka consumer的消費策略配置

val helloStream: FlinkKafkaConsumer011[String] new FlinkKafkaConsumer011[String]("hello", valueDeserializer, kafkaProps) // 指定消費策略 helloStream.setStartFromEarliest() // - 從最早的記錄開始&#xff1b; helloStream.setStartFromLatest() //…

spdk/dpdk 編譯相關問題匯總

下載 到官網上下載最新的spdk 代碼。 解決編譯依賴 yum install libaio.x86_64 libaio-devel.x86_64 編譯dpdk 特別注意的是dpdk:依賴 /lib/modules/uname -a/build 下執行的內核已經存在&#xff0c;并且編譯過&#xff0c;為此需要&#xff1a; 下載內核&#xff1b;安裝依賴…

深入淺出 消息隊列 ActiveMQ

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一、 概述與介紹 ActiveMQ 是Apache出品&#xff0c;最流行的、功能強大的即時通訊和集成模式的開源服務器。ActiveMQ 是一個完全支持JM…

2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) - 4.28

賽后補了幾道 賽中我就寫了兩個... A - Altruistic AmphibiansGym - 101933A 看了眼榜沒幾個人做。就沒看。 最后發現就是一個DP&#xff08;但是我覺得復雜度有點迷&#xff09; 題意&#xff1a;$n$只青蛙有參數$l,w,h$分別表示彈跳力&#xff0c;體重&#xff0c;身高&#…

消息隊列技術介紹 : ActiveMQ、RabbitMQ、ZeroMQ、Kafka、MetaMQ、RocketMQ

一、 消息隊列概述 前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 消息隊列中間件是分布式系統中重要的組件&#xff0c;主要解決應用耦合、異步消息、流量削鋒等問題。實現高性能、高可…

程序員的惡性循環 !

窮人的惡性循環&#xff1a; 窮 -> 需要努力工作 -> 沒有時間去交際 -> 人脈越來越狹窄 -> 工作越來越難做 -> 越需要努力去工作 -> 越沒有時間去發展人脈 -> 越窮 富人的良性循環&#xff1a; 有錢 -> 工作很輕松 -> 很多時間都在交際上 -> 人…

刷臉考勤,重新定位校園管理

近幾年&#xff0c;人臉識別技術在安防領域得到了廣泛應用&#xff0c;隨著技術的不斷發展&#xff0c;它離我們的日常生活越來越近&#xff0c;手機、商場、公園、校園等都可以看到它的身影。刷臉考勤&#xff0c;重新定義校園管理。人臉識別&#xff0c;也叫面部識別&#xf…

python爬蟲學習之頁面登陸

爬蟲學習的一點心得 登陸主要有3種方法&#xff1a;使用selenium&#xff0c;cookies&#xff0c;模擬表單登陸 個人對于一般情況使用cookies登陸 可以實現一次手動&#xff0c;長期自動&#xff0c;可以繞過登陸&#xff08;登陸的相關信息密碼&#xff0c;賬號等會存于cookie…

消息隊列 應用場景 解析

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 另外騰訊云-云社區還有一文不允許轉載&#xff0c;但內容挺好的&#xff1a;https://cloud.tencent.com/developer/article/1006035 分布…

求職面試的時候如何談薪酬待遇

在社會大學里混了那么多年&#xff0c;我最慘痛的經歷就是&#xff0c;在應聘一家企業的時候&#xff0c;總是羞于談薪酬待遇。大概這是很多職場新人都會遇到過的尷尬吧——覺得自己經驗不夠&#xff0c;或者想應聘的企業比較好&#xff0c;就覺得對方提多少就是多少吧&#xf…

利用memcached實現CAS單點登錄集群部署

前言&#xff1a;利用memcached實現CAS單點登錄集群部署 負載均衡&#xff1a;將接口請求的有狀態性變成無狀態性。是我們在實現負載均衡時必要要解決的問題。以應用接口的session狀態為例&#xff0c;一般解決方法都是將session數據和應用進行剝離&#xff0c;session數據統一…

注冊

<!DOCTYPE html><html lang"en"><head> <meta charset"UTF-8"> <title>注冊</title> {# 導入jQuery基礎類庫&#xff0c;才可以使用 $ #} <script src"../static/js/jquery-1.12.4.min.js"&…