您好,登錄后才能下訂單哦!
使用PDO編寫一個(gè)學(xué)生管理系統(tǒng)?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
需要建立如下文件:
index.php
menu.php //菜單欄
add.php //添加數(shù)據(jù)
edit.php // 編輯數(shù)據(jù)
action.php // 添加,刪除,編輯的實(shí)現(xiàn)
分別寫一下每個(gè)文件的代碼:
menu.php:
<html> <h3>學(xué)生信息管理</h3> <a href="index.php" rel="external nofollow" >瀏覽學(xué)生</a> <a href="add.php" rel="external nofollow" >增加學(xué)生</a> <hr> </html>
index.php
<html> <head> <meta charset="UTF-8"> <title>學(xué)生信息管理系統(tǒng)</title> </head> <script> function doDel(id){ if(confirm("是否要?jiǎng)h除")){ window.location='action.php?action=del&id='+id; } } </script> <body> <center> <?php include("menu.php");?> <h4>瀏覽學(xué)生信息</h4> <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>學(xué)生信息管理系統(tǒng)</title> </head> <body> <center> <?php include("menu.php");?> <h4>增加學(xué)生信息</h4> <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>學(xué)生信息管理系統(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("沒有修改的信息"); } ?> <h4>修改學(xué)生信息</h4> <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)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。