最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP 結(jié)合 Boostrap 結(jié)合 js 實(shí)現(xiàn)學(xué)生列表刪除編輯及搜索功能

 更新時(shí)間:2019年05月21日 14:35:20   作者:小方哥·  
這篇文章主要介紹了PHP 結(jié)合 Boostrap 結(jié)合 js 實(shí)現(xiàn)學(xué)生列表刪除編輯以及搜索功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

這個(gè)自己的小項(xiàng)目要先告一段落了??赡苓€有許多bug。請(qǐng)見(jiàn)諒 

刪除學(xué)生功能

PHP:

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

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

HTML:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>編輯學(xué)生</title>
  <link rel="stylesheet" type="text/css" href="css/Bootstrap.css" rel="external nofollow" >
</head>
<body>
  <div class="container mt-3">
    <h1 class="display-5 text-center">編輯學(xué)生</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="學(xué)號(hào)" value="<?php echo isset($_POST['num']) ? $_POST['num'] : $current_student['num']; ?>">
        </div>
        <div class="form-group">
          <select class="form-control" name="system">
            <option>請(qǐng)選擇學(xué)院/系</option>
            <option <?php echo $current_student['system'] === '電氣工程學(xué)院' ? 'selected' : ''; ?>>電氣工程學(xué)院</option>
            <option <?php echo $current_student['system'] === '信息工程與藝術(shù)學(xué)院' ? 'selected' : ''; ?>>信息工程與藝術(shù)學(xué)院</option>
            <option <?php echo $current_student['system'] === '國(guó)際教育學(xué)院' ? 'selected' : ''; ?>>國(guó)際教育學(xué)院</option>
            <option <?php echo $current_student['system'] === '水利水電工程學(xué)院' ? 'selected' : ''; ?>>水利水電工程學(xué)院</option>
            <option <?php echo $current_student['system'] === '測(cè)繪與市政工程學(xué)院' ? 'selected' : ''; ?>>測(cè)繪與市政工程學(xué)院</option>
            <option <?php echo $current_student['system'] === '馬克思主義學(xué)院' ? 'selected' : ''; ?>>馬克思主義學(xué)院</option>
            <option <?php echo $current_student['system'] === '建筑工程學(xué)院' ? 'selected' : ''; ?>>建筑工程學(xué)院</option>
            <option <?php echo $current_student['system'] === '經(jīng)濟(jì)與管理學(xué)院' ? 'selected' : ''; ?>>經(jīng)濟(jì)與管理學(xué)院</option>
          </select>
        </div>
        <div class="form-group">
          <input type="text" name="class" class="form-control" placeholder="班級(jí)" 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">請(qǐng)選擇性別</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">確認(rèn)修改</button>
      </form>
    </div>
  </div>
</body>
</html>

PHP:

if (empty($_GET['id'])) exit('<h1>必須指定相應(yīng)的學(xué)號(hào)</h1>');
$current_num = $_GET['id'];
$connection = mysqli_connect('localhost', 'root', '密碼', 'students_info_system');
if (!$connection) exit('<h1>連接數(shù)據(jù)庫(kù)失敗</h1>');
$query = mysqli_query($connection, "select * from students where num = {$current_num} limit 1");
if (!$query) exit('<h1>找不到您要編輯的學(xué)生信息</h1>');
$current_student = mysqli_fetch_assoc($query);
// var_dump($current_student);
function edit_student() {
  // var_dump('進(jìn)來(lái)了');
  global $connection;
  global $current_num;  // 當(dāng)前學(xué)生學(xué)號(hào)
  global $current_student;
  $extra_students_query = mysqli_query($connection, "select * from students where num != {$current_num}");
  if (!$extra_students_query) {
    exit('<h1>其余學(xué)生數(shù)據(jù)查詢失敗</h1>');
    // return;
  }
  // 查詢除該學(xué)生以外的其他學(xué)生
  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'] = '請(qǐng)輸入學(xué)號(hào)';
    return;
  }
  // 判斷該學(xué)號(hào)是否已經(jīng)被添加(即列表中已存在該學(xué)生)=========
  if (in_array($_POST['num'], $students_num)) {
    $GLOBALS['error_msg'] = '該學(xué)生已存在';
    return;
  }
  if (empty($_POST['system']) || $_POST['system'] === '請(qǐng)選擇學(xué)院/系') {
    $GLOBALS['error_msg'] = '請(qǐng)選擇學(xué)院/系';
    return;
  }
  if (empty($_POST['class'])) {
    $GLOBALS['error_msg'] = '請(qǐng)輸入班級(jí)';
    return;
  }
  if (empty($_POST['name'])) {
    $GLOBALS['error_msg'] = '請(qǐng)輸入姓名';
    return;
  }
  if (!(isset($_POST['gender']) && $_POST['gender'] !== '-1')) {
    $GLOBALS['error_msg'] = '請(qǐng)選擇性別';
    return;
  }
  if (empty($_POST['birthday'])) {
    $GLOBALS['error_msg'] = '請(qǐng)輸入出生日期';
    return;
  }
  // 以下處理文件域=======================================================
  // 當(dāng)有文件上傳時(shí)才驗(yàn)證,沒(méi)有上傳則照片不變
  // $_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;
    }
    // 驗(yàn)證上傳文件的類型(只允許圖片)
    if (strpos($_FILES['photo']['type'], 'image/') !== 0) {
      $GLOBALS['error_msg'] = '這不是支持的文件格式類型,請(qǐng)重新上傳';
      return;
    }
    // 文件上傳到了服務(wù)端開(kāi)辟的一個(gè)臨時(shí)地址,需要轉(zhuǎn)移到本地
    $image_target = 'images/' . $_FILES['photo']['name'];
    if (!move_uploaded_file($_FILES['photo']['tmp_name'], $image_target)) {
      $GLOBALS['error_msg'] = '圖片上傳失敗';
      return;
    }
    // 接收更新過(guò)的學(xué)生照片
    $current_student['photo'] = (string)$image_target;
  } else {
    // var_dump($_FILES['photo']);
    // 如果照片沒(méi)有上傳則不進(jìn)行驗(yàn)證文件域,直接更新數(shù)據(jù)且不改變?cè)瓉?lái)的照片
    $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);
  // 將數(shù)據(jù)修改存放到數(shù)據(jù)庫(kù)
  $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'] = '更新數(shù)據(jù)查詢失敗';
    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)

// 關(guān)鍵詞搜索功能----立即函數(shù)
(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;             // 獲取隱藏的學(xué)生個(gè)數(shù)(即表格隱藏行數(shù))
    // 獲取要搜索的關(guān)鍵詞
    const search_content = document.getElementById(search_key).value;
    // console.log(search_content);
    // console.log(typeof(search_content));
    let data = [];
    // 遍歷列表將數(shù)據(jù)存儲(chǔ)到一個(gè)數(shù)組中
    // 1.獲取表格行數(shù)
    for (let i = 0; i < table.children.length; i++) {
      // 2.獲取表格列數(shù)
      for (let j = 0; j < table.children[i].children.length; j++) {
        if (!data[i]) {
          // 在數(shù)組中創(chuàng)鍵每一行內(nèi)容存放的數(shù)組,用于存放一行數(shù)據(jù)
          data[i] = new Array();
        }
        data[i][j] = table.children[i].children[j].innerHTML.toString();
        // 3.存放數(shù)據(jù)
        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) + "名學(xué)生";
    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 + "名學(xué)生";
  document.getElementById('students_number').innerHTML = str;
})('search', 'search-key');

同時(shí)在原有的學(xué)生信息頁(yè)面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" rel="external nofollow" >添加學(xué)生</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="請(qǐng)輸入關(guān)鍵詞" value="" id="search-key">
       <button type="submit" class="btn btn-info col-sm-2 ml-2" id="search">點(diǎn)擊搜索</button>
    </div>

總結(jié)

以上所述是小編給大家介紹的PHP 結(jié)合 Boostrap 結(jié)合 js 實(shí)現(xiàn)學(xué)生列表刪除編輯及搜索功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • yii2.0整合阿里云oss上傳單個(gè)文件的示例

    yii2.0整合阿里云oss上傳單個(gè)文件的示例

    本篇文章主要介紹了yii2.0整合阿里云oss上傳單個(gè)文件的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • 使用Git實(shí)現(xiàn)Laravel項(xiàng)目的自動(dòng)化部署

    使用Git實(shí)現(xiàn)Laravel項(xiàng)目的自動(dòng)化部署

    這篇文章主要介紹了使用Git實(shí)現(xiàn)Laravel項(xiàng)目的自動(dòng)化部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • smarty模板引擎基礎(chǔ)知識(shí)入門(mén)

    smarty模板引擎基礎(chǔ)知識(shí)入門(mén)

    這篇文章主要介紹了smarty模板引擎基礎(chǔ)知識(shí)入門(mén),較為詳細(xì)的分析了smarty的基本概念并實(shí)例分析了相關(guān)的基本用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Laravel 5框架學(xué)習(xí)之表單

    Laravel 5框架學(xué)習(xí)之表單

    Laravel 5.0 新引入的表單請(qǐng)求 (Form Request) 特性提供了集規(guī)范性 (差不多就是 "最佳實(shí)踐" 的意思) 和便捷性 (這是比之前任何一種選擇都更強(qiáng)大也更便捷的方式) 于一體的, 在 Laravel 中執(zhí)行數(shù)據(jù)檢查和驗(yàn)證的新手段.
    2015-04-04
  • Yii2.0使用阿里云OSS的SDK上傳圖片、下載、刪除圖片示例

    Yii2.0使用阿里云OSS的SDK上傳圖片、下載、刪除圖片示例

    本篇文章主要介紹了Yii2.0使用阿里云OSS的SDK上傳圖片、下載、刪除圖片示例,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • php自定義的格式化時(shí)間示例代碼

    php自定義的格式化時(shí)間示例代碼

    時(shí)間剛好是5分鐘前,則對(duì)應(yīng)的時(shí)間戳就會(huì)被格式化為5分鐘前,自定義的格式化時(shí)間方法如下,感興趣的朋友可以參考下
    2013-12-12
  • php獲取網(wǎng)頁(yè)中圖片、DIV內(nèi)容的簡(jiǎn)單方法

    php獲取網(wǎng)頁(yè)中圖片、DIV內(nèi)容的簡(jiǎn)單方法

    這篇文章主要介紹了php獲取網(wǎng)頁(yè)中圖片、DIV內(nèi)容的簡(jiǎn)單方法,都是通過(guò)正則表達(dá)式實(shí)現(xiàn)的,強(qiáng)大的正則啊,需要的朋友可以參考下
    2014-06-06
  • Yii框架登錄流程分析

    Yii框架登錄流程分析

    這篇文章主要介紹了Yii框架登錄流程,詳細(xì)的分析了Yii框架的原理與登錄的詳細(xì)流程,有助于深入了解Yii框架的原理,需要的朋友可以參考下
    2014-12-12
  • laravel框架創(chuàng)建授權(quán)策略實(shí)例分析

    laravel框架創(chuàng)建授權(quán)策略實(shí)例分析

    這篇文章主要介紹了laravel框架創(chuàng)建授權(quán)策略,結(jié)合實(shí)例形式分析了laravel框架創(chuàng)建授權(quán)策略的相關(guān)步驟、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2019-11-11
  • php單例模式的簡(jiǎn)單實(shí)現(xiàn)方法

    php單例模式的簡(jiǎn)單實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇php單例模式的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06

最新評(píng)論

吉隆县| 剑川县| 霍山县| 晋州市| 双城市| 南溪县| 赤城县| 来宾市| 嘉义市| 涿鹿县| 闸北区| 日土县| 嘉祥县| 连山| 津南区| 二连浩特市| 如皋市| 浦北县| 巴中市| 扬中市| 林甸县| 本溪市| 山东| 高青县| 灵山县| 伊金霍洛旗| 通渭县| 深水埗区| 双江| 元江| 潼南县| 荔浦县| 全椒县| 库车县| 九龙县| 新营市| 尼玛县| 农安县| 揭西县| 灵丘县| 仁怀市|