PHP 結合 Bootstrap 實現學生列表以及添加學生功能實現(繼上篇登錄及註冊功能之後)

来源:https://www.cnblogs.com/duxiu-fang/archive/2019/05/18/10886998.html
-Advertisement-
Play Games

本人是一位學生,正在學習當中,可能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(); }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 環境ubutun14,python版本是python3.6. 今天在安裝Pip 時出現ModuleNotFoundError: No module named 'distutils.util'。操作步驟如下: 我們可以通過以下命令來判斷是否已安裝: 如果還未安裝,則可以使用以下方法來安裝: 註意:用 ...
  • // ConsoleApplication1.cpp: 定義控制台應用程式的入口點。//#include "stdafx.h"#include <iostream>#include <string>using namespace std; int countnubstr(string str){ i ...
  • 我們在項目開發中有很多地方使用到了註解,關於註解的定義與創建小伙伴可以參考我的文章《java註解》。有任何問題的小伙伴們可以在評論區指出哦,歡迎各位大佬指出問題。 今天我要說的是使用註解與反射結合使用,來使我們代碼根據優雅,更加高大上(咳,裝逼神器啊)。 註解使用@interface 來定義,辣麽我 ...
  • [轉載] Python數據類型知識點全解 1.字元串 字元串常用功能 字元串的內置方法 2.列表 3.元組 4.字典 5.集合 ...
  • 1041. 困於環中的機器人 題庫鏈接: 1041. 困於環中的機器人. 題乾 在無限的平面上,機器人最初位於 (0, 0) 處,面朝北方。機器人可以接受下列三條指令之一: "G":直走 1 個單位 "L":左轉 90 度 "R":右轉 90 度 機器人按順序執行指令 instructions,並一 ...
  • //原文參考https://blog.csdn.net/lanchunhui/article/details/52503332 你以為你定義了一個類的對象,其實在編譯器看來你是聲明瞭一個函數 修改為: 當構造函數中存在一些參數時: 當構造函數的參數帶預設值: ...
  • [toc] 創建ashop sso web單點登陸系統 先創建好模塊, 然後配置pom.xml文件 加入spring的配置文件 然後配置web.xml文件, 並把靜態資源放到webapp目錄下. 打包部署模塊測試, 能看到如下效果則項目搭建成功.   用戶名唯一性驗證 |請求方法|GET| ...
  • day23 03 組合的例子 一、用到組合的方式,編寫一個圓環,並能夠計算出它的周長和麵積 運行結果: 二、創建一個老師類,老師有生日,生日也是一個類,涉及組合的方法 運行結果: 三、複習 1、面向對象編程 思想:角色的抽象,創建類,創建角色(實例化),操作這些實例 關鍵字:class 基本框架: ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...