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

PDO實現(xiàn)學生管理系統(tǒng)

 更新時間:2020年03月21日 13:01:24   作者:thystar  
這篇文章主要為大家詳細介紹了PDO實現(xiàn)學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這里實現(xiàn)一個簡單的學生管理系統(tǒng),供大家參考,具體內(nèi)容如下

需要建立如下文件:

  • index.php
  • menu.php //菜單欄
  • add.php  //添加數(shù)據(jù)
  • edit.php // 編輯數(shù)據(jù)
  • action.php // 添加,刪除,編輯的實現(xiàn)

分別寫一下每個文件的代碼:

menu.php:

<html>
<h2>學生信息管理</h2>
<a href="index.php" rel="external nofollow" >瀏覽學生</a>
<a href="add.php" rel="external nofollow" >增加學生</a>
<hr>
</html>

index.php

<html>
 <head>
  <meta charset="UTF-8">
  <title>學生信息管理系統(tǒng)</title>
 </head>
 <script>
  function doDel(id){
   if(confirm("是否要刪除")){
    window.location='action.php?action=del&id='+id;
   }
  }
 </script>
 <body>
  <center>
   <?php include("menu.php");?>
   <h3>瀏覽學生信息</h3>
   <table width="600" border="1">
    <tr>
     <th>ID</th>
     <th>姓名</th>
     <th>姓別</th>
     <th>年齡</th>
     <th>班級</th>
     <th>操作</th>
    </tr>
    <?php
     //1. 連接數(shù)據(jù)庫
     try{
      $pdo = new PDO("mysql:host=localhost;dbname=myapp;", "root", "");
 
     }catch(PDOException $e){
      die("fail to connect db".$e->getMessage());
     }
     //2. 執(zhí)行數(shù)據(jù)庫,并解析遍歷
     $sql = "SELECT * FROM users";
     foreach($pdo->query($sql) as $val){
      echo "<tr>";
      echo "<td>{$val['id']}</td>";
      echo "<td>{$val['name']}</td>";
      echo "<td>{$val['sex']}</td>";
      echo "<td>{$val['age']}</td>";
      echo "<td>{$val['class']}</td>";
      echo "<td>
         <a href='javascript:doDel({$val['id']})'>刪除</a>
         <a href='edit.php?id={$val['id']}'>修改</a>
        </td>";
      echo "</tr>";
     }
    ?>
   </table>
 
  </center>
 </body>
</html>

add.php

<html>
<head>
 <meta charset="UTF-8">
 <title>學生信息管理系統(tǒng)</title>
</head>
<body>
<center>
 <?php include("menu.php");?>
 <h3>增加學生信息</h3>
 <form action="action.php?action=add" method="post">
  <table>
   <tr>
    <td>姓名</td>
    <td><input type="text" name="name"/></td>
   </tr>
 
   <tr>
    <td>姓別</td>
    <td>
     <input type="radio" name="sex" value="m"/>男
     <input type="radio" name="sex" value="w"/>女
    </td>
 
   </tr>
 
   <tr>
    <td>年齡</td>
    <td><input type="text" name="age"/></td>
   </tr>
 
   <tr>
    <td>班級</td>
    <td><input type="text" name="class"/></td>
   </tr>
 
   <tr>
    <td> </td>
    <td>
     <input type="submit" value="增加"/>
     <input type="submit" value="重置"/>
    </td>
 
   </tr>
  </table>
 </form>
</center>
</body>
</html>

edit.php

<html>
<head>
 <meta charset="UTF-8">
 <title>學生信息管理系統(tǒng)</title>
</head>
<body>
<center>
 <?php include("menu.php");
 //獲取修改信息
 //1. 連接數(shù)據(jù)庫
 try{
  $pdo = new PDO("mysql:host=localhost;dbname=myapp;", "root", "");
 
 }catch(PDOException $e){
  die("fail to connect db".$e->getMessage());
 }
 //2. 拼裝sql語句,取出信息
 $sql = "SELECT * FROM users WHERE id=".$_GET['id'];
 $stmt = $pdo->query($sql);
 if($stmt->rowCount() > 0){
  $stu = $stmt->fetch(PDO::FETCH_ASSOC); //解析數(shù)據(jù)
 }else{
  die("沒有修改的信息");
 }
 ?>
 <h3>修改學生信息</h3>
 <form action="action.php?action=edit" method="post">
 <!-- 以隱藏域的方式添加id  -->
  <input type="hidden" name="id" value="<?php echo $stu['id']; ?>">
  <table>
   <tr>
    <td>姓名</td>
    <td><input type="text" name="name" value="<?php echo $stu['name'];?>"/></td>
   </tr>
 
   <tr>
    <td>姓別</td>
    <td>
     <input type="radio" name="sex" value="m" <?php echo ($stu['sex']==
      "m")? "checked": ""; ?>/>男
     <input type="radio" name="sex" value="w" <?php echo ($stu['sex']==
      "w")? "checked": ""; ?>/>女
    </td>
 
   </tr>
 
   <tr>
    <td>年齡</td>
    <td><input type="text" name="age" value="<?php echo $stu['age'];?>"/></td>
   </tr>
 
   <tr>
    <td>班級</td>
    <td><input type="text" name="class" value="<?php echo $stu['class'];?>"/></td>
   </tr>
 
   <tr>
    <td> </td>
    <td>
     <input type="submit" value="修改"/>
     <input type="submit" value="重置"/>
    </td>
 
   </tr>
  </table>
 </form>
</center>
</body>
</html>

action.php

<?php
//1. 連接數(shù)據(jù)庫
try{
 $pdo = new PDO("mysql:host=localhost;dbname=myapp;", "root", "");
 
}catch(PDOException $e){
 die("fail to connect db".$e->getMessage());
}
//2. 通過action的值做相應(yīng)的操作
switch($_GET['action']){
 case "add": //增加操作
  $name = $_POST['name'];
  $sex = $_POST['sex'];
  $age = $_POST['age'];
  $class = $_POST['class'];
 
  $sql = "INSERT INTO users VALUES (null, '{$name}','{$sex}', '{$age}', '{$class}')";
  $rw = $pdo->exec($sql);
  if($rw > 0){
   echo "<script>alert('增加成功'); window.location='index.php'</script>";
  }else{
   echo "<script>alert('增加失敗'); window.history.back()</script>";
  }
  break;
 case "del":
  $id = $_GET['id'];
  $sql = "DELETE FROM users WHERE id={$id}";
  $pdo->exec($sql);
  header("location:index.php");
  break;
 case "edit":
  $name = $_POST['name'];
  $sex = $_POST['sex'];
  $age = $_POST['age'];
  $class = $_POST['class'];
  $id = $_POST['id'];
 
  $sql = "UPDATE users SET name='{$name}',sex='{$sex}',age={$age},class={$class}
    WHERE id={$id}";
  $rw = $pdo->exec($sql);
  if($rw > 0){
   echo "<script>alert('修改成功'); window.location='index.php'</script>";
  }else{
   echo "<script>alert('修改失敗'); window.history.back()</script>";
  }
  break;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • 8個必備的PHP功能實例代碼

    8個必備的PHP功能實例代碼

    本文將分享8個開發(fā)必備的PHP功能,個個都非常實用:傳遞任意數(shù)量的函數(shù)參數(shù) 、使用glob()查找文件、獲取內(nèi)存使用情況信息、獲取CPU使用情況信息 、獲取系統(tǒng)常量 、生成唯一的id 、序列化 、字符串壓縮。很實用的8個PHP功能。
    2013-10-10
  • codeigniter中view通過循環(huán)顯示數(shù)組數(shù)據(jù)的方法

    codeigniter中view通過循環(huán)顯示數(shù)組數(shù)據(jù)的方法

    這篇文章主要介紹了codeigniter中view通過循環(huán)顯示數(shù)組數(shù)據(jù)的方法,實例分析了codeigniter中view方法與數(shù)組遍歷的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • PHP實現(xiàn)百度人臉識別

    PHP實現(xiàn)百度人臉識別

    這篇文章主要為大家詳細介紹了PHP實現(xiàn)百度人臉識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 避免Smarty與CSS語法沖突的方法

    避免Smarty與CSS語法沖突的方法

    這篇文章主要介紹了避免Smarty與CSS語法沖突的方法,實例分析了Smarty與CSS中大括號{}沖突的處理技巧,需要的朋友可以參考下
    2015-03-03
  • php計算多個集合的笛卡爾積實例詳解

    php計算多個集合的笛卡爾積實例詳解

    笛卡爾積又叫笛卡爾乘積,是一個叫笛卡爾的人提出來的。 簡單的說就是兩個集合相乘的結(jié)果。具體的定義要看看有關(guān)代數(shù)系的書的定義。這篇文章主要給大家介紹了利用php計算多個集合的笛卡爾積的方法實例,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別

    PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別

    這篇文章主要介紹了PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別,以及一個SQL注入漏洞介紹,需要的朋友可以參考下
    2014-04-04
  • ThinkPHP+EasyUI之ComboTree中的會計科目樹形菜單實現(xiàn)方法

    ThinkPHP+EasyUI之ComboTree中的會計科目樹形菜單實現(xiàn)方法

    下面小編就為大家?guī)硪黄猅hinkPHP+EasyUI之ComboTree中的會計科目樹形菜單實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • PHP文件系統(tǒng)管理(實例講解)

    PHP文件系統(tǒng)管理(實例講解)

    下面小編就為大家?guī)硪黄狿HP文件系統(tǒng)管理(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • PHP+jquery+CSS制作頭像登錄窗(仿QQ登陸)

    PHP+jquery+CSS制作頭像登錄窗(仿QQ登陸)

    本篇文章介紹了PHP結(jié)合jQ和CSS制作頭像登錄窗(仿QQ登陸),實現(xiàn)了類似QQ的登陸界面,很有參考價值,有需要的朋友可以了解一下。
    2016-10-10
  • destoon實現(xiàn)資訊信息前面調(diào)用它所屬分類的方法

    destoon實現(xiàn)資訊信息前面調(diào)用它所屬分類的方法

    這篇文章主要介紹了destoon實現(xiàn)資訊信息前面調(diào)用它所屬分類的方法,在模板制作中非常實用,需要的朋友可以參考下
    2014-07-07

最新評論

温州市| 许昌市| 阿图什市| 那坡县| 芒康县| 藁城市| 苏尼特左旗| 建德市| 长子县| 揭阳市| 逊克县| 西乌| 宣汉县| 新兴县| 双柏县| 宜都市| 定结县| 婺源县| 莱西市| 旺苍县| 济宁市| 延长县| 涡阳县| 紫金县| 楚雄市| 泸溪县| 北宁市| 图木舒克市| 那坡县| 怀安县| 黄平县| 广汉市| 揭阳市| 桃园县| 湟中县| 武定县| 海兴县| 班玛县| 正蓝旗| 汉阴县| 电白县|