php程式碼怎麼實現成績查詢
php程式碼實現成績查詢的方法:1、建立前端登入頁面程式碼;2、透過「if (isset($_SESSION['username'])) {...}」語法實作判斷使用者是否登入;3、建立後端管理登入頁面;4、連接資料庫;5、透過「session_start(); if (isset($_COOKIE['username'])) {$_SESSION['']}」代碼實現查詢成績即可。
本教學操作環境:Windows7系統、PHP8.1版、Dell G3電腦。
php程式碼怎麼實現成績查詢?
PHP成績查詢系統
一個非常簡陋的PHP成績查詢系統,期末作業。
因為上課打醬油了,所以這也是最後幾天搗鼓出來的,程式碼都是東拼西湊的,只有簡單的增刪改查功能。就醬紫。
資料庫:
#總共這麼多檔(html、css、php都寫一塊了)
然後介面:(就長這樣)
#程式碼是按上圖的檔案順序排的
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0px; padding: auto; } fieldset { margin: auto; margin-top: 200px; width: 400px; text-align: center; } ul li { margin: 0; padding: 0; } form { margin: 40px 30px 0; } form li { list-style: none; padding: 5px 0; } .login_btn { border: none; background: #01A4F1; color: #fff; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; padding: 0 10px; cursor: pointer; } a:link { text-decoration: none; color: blue; } a:visited { color: blue; text-decoration: none; } .return_but { float: right; } </style> </head> <body> <form action="#" method="POST"> <fieldset> <legend>添加学生成绩</legend> <ul> <li> 请输入学生的<b>成绩</b> <span><a href="show_teacher.php">返回</a></span> </li> <li> 学号: <input type="text" name="username" /> </li> <li> 语文: <input type="text" name="yuwen" /> </li> <li> 数学: <input type="text" name="shuxue" /> </li> <li> 英语: <input type="text" name="yingyu" /> </li> <li> 综合: <input type="text" name="zonghe" /> </li> <li> <input type="submit" name="add_score" value="确认添加" /> </li> </ul> </fieldset> </form> <?php header("Content-Type:text/html; charset=utf-8"); session_start(); include("conn.php"); $con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem"); mysqli_query($con, "set names utf8"); if (isset($_SESSION['username'])) { if (isset($_POST['add_score'])) { $username = $_POST['username']; $yuwen = $_POST['yuwen']; $shuxue = $_POST['shuxue']; $yingyu = $_POST['yingyu']; $zonghe = $_POST['zonghe']; $sql = "insert into score (username,语文,数学,英语,综合) values('$username','$yuwen','$shuxue','$yingyu','$zonghe')"; mysqli_query($con, $sql); if (mysqli_affected_rows($con) > 0) { echo "<script> alert('添加成功'); location.href='show_teacher.php';</script>"; } } } else { //缓存意外被清除后、 echo "用户信息丢失,3秒后返回登陆界面"; header('refresh: 3; url=index.php'); } ?> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0px; padding: auto; } fieldset { margin: auto; margin-top: 200px; width: 400px; text-align: center; } ul li { margin: 0; padding: 0; } form { margin: 40px 30px 0; } form li { list-style: none; padding: 5px 0; } .login_btn { border: none; background: #01A4F1; color: #fff; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; padding: 0 10px; cursor: pointer; } a:link { text-decoration: none; color: blue; } a:visited { color: blue; text-decoration: none; } .return_but { float: right; } </style> </head> <body> <form action="#" method="POST"> <fieldset> <legend>添加学生信息</legend> <ul> <li> 请输入需要添加学生的<b>学号</b>和<b>登陆密码</b> <span><a href="show_teacher.php">返回</a></span> </li> <li> 学号: <input type="text" name="username" /> </li> <li> 密码: <input type="password" name="password" /> </li> <li> <input type="submit" name="add_student" value="确认添加" /> </li> </ul> </fieldset> </form> <?php header("Content-Type:text/html; charset=utf-8"); session_start(); include("conn.php"); $con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem"); mysqli_query($con, "set names utf8"); if (isset($_SESSION['username'])) { if (isset($_POST['add_student'])) { $username = $_POST['username']; $password = $_POST['password']; $sql = "insert into student (username,password) values('$username','$password')"; mysqli_query($con, $sql); if (mysqli_affected_rows($con) > 0) { echo "<script> alert('添加成功'); location.href='show_teacher.php';</script>"; } } } else { //缓存意外被清除后、 echo "用户信息丢失,3秒后返回登陆界面"; header('refresh: 3; url=index.php'); } ?> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0px; padding: auto; } fieldset { margin: auto; margin-top: 200px; width: 400px; text-align: center; } ul li { margin: 0; padding: 0; } form { margin: 40px 30px 0; } form li { list-style: none; padding: 5px 0; } .login_btn { border: none; background: #01A4F1; color: #fff; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; padding: 0 10px; cursor: pointer; } a:link { text-decoration: none; color: blue; } a:visited { color: blue; text-decoration: none; } .return_but { float: right; } </style> </head> <body> <form action="#" method="POST"> <fieldset> <legend>修改学生成绩</legend> <ul> <li> 学号:<?php echo $_GET['username'] ?> <span><a href="show_teacher.php">返回</a></span> </li> <li> 语文: <input type="text" name="yuwen" value="<?php echo $_GET['yuwen'] ?>" /> </li> <li> 数学: <input type="text" name="shuxue" value="<?php echo $_GET['shuxue'] ?>" /> </li> <li> 英语: <input type="text" name="yingyu" value="<?php echo $_GET['yingyu'] ?>" /> </li> <li> 综合: <input type="text" name="zonghe" value="<?php echo $_GET['zonghe'] ?>" /> </li> <li> <input type="submit" name="alter_score" value="确认修改" /> </li> </ul> </fieldset> </form> <?php header("Content-Type:text/html; charset=utf-8"); session_start(); include("conn.php"); $con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem"); mysqli_query($con, "set names utf8"); if (isset($_SESSION['username'])) { if (isset($_POST['alter_score'])) { $username = $_GET['username']; $yuwen = $_POST['yuwen']; $shuxue = $_POST['shuxue']; $yingyu = $_POST['yingyu']; $zonghe = $_POST['zonghe']; $sql = "UPDATE `score` SET`语文`=$yuwen,`数学`=$shuxue,`英语`=$yingyu,`综合`=$zonghe WHERE username = $username"; mysqli_query($con, $sql); if (mysqli_affected_rows($con) > 0) { echo "<script> alert('修改成功'); location.href='show_teacher.php';</script>"; } } } else { //缓存意外被清除后、 echo "用户信息丢失,3秒后返回登陆界面"; header('refresh: 3; url=index.php'); } ?> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <?php $con =mysqli_connect("localhost:3306","root","","miniblog"); if(!$con){ die("链接错误"); } mysqli_query($con,"set names utf8"); ?> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <?php header("Content-Type:text/html; charset=utf-8"); session_start(); include("conn.php"); $con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem"); mysqli_query($con, "set names utf8"); if (isset($_SESSION['username'])) { if (isset($_GET['id'])) { $username = $_GET['id']; echo $username; $sql = "delete from score where username = $username"; mysqli_query($con, $sql); if (mysqli_affected_rows($con) > 0) { echo "<script> alert('删除成功'); location.href='show_teacher.php';</script>"; } } } else { //缓存意外被清除后、 echo "用户信息丢失,3秒后返回登陆界面"; header('refresh: 3; url=index.php'); } ?> </body> </html>
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> * { margin: 0px; padding: auto; } fieldset { margin: auto; margin-top: 200px; width: 400px; text-align: center; } ul li { margin: 0; padding: 0; } form { margin: 40px 30px 0; } form li { list-style: none; padding: 5px 0; } .login_btn { border: none; background: #01A4F1; color: #fff; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; padding: 0 10px; cursor: pointer; } </style> </head> <body> <form action="#" method="POST"> <fieldset> <legend>用户登录</legend> <ul> <li> 用户名: <input type="text" name="username" /> </li> <li> 密 码: <input type="password" name="password" /> </li> <li> <input type="submit" name="login_student" value="学生登录" /> <input type="submit" name="login_teacher" value="教师登录" /> </li> </ul> <?php header("Content-Type:text/html;charset=utf-8"); include("conn.php"); $con = mysqli_connect("localhost:3306", "root", "", "resultsquerysystem"); session_start(); //点击学生登陆按钮 if (isset($_POST['login_student'])) { $username = trim($_POST['username']); $password = trim($_POST['password']); if (($username == '') || ($password == '')) { header('refresh: 3; url=index.php'); echo "该用户名或者密码不能为空,3秒后跳转到登录页面"; exit; } else { $sql = "select * from student where username='$username'"; $res = mysqli_query($con, $sql); $n = mysqli_num_rows($res); if ($n > 0) { $row = mysqli_fetch_assoc($res); $pwd = $row['password']; //用户名或密码错误 if ($password != $pwd) { # code... header('refresh: 3; url=index.php'); echo "用户名或者密码错误,3秒后跳转到登录页面"; } else { //登录成功,将用户信息保存到session中 $_SESSION['username'] = $username; $_SESSION['islogin'] = 1; //用户信息保存到Cookie ,1天 setcookie("username", $username, time() + 24 * 60 * 60); setcookie( "pw", md5($username . md5($password)), time() + 24 * 60 * 60 ); //跳转到显示页面 header("location:show_student.php"); } } else { header('refresh: 3; url=index.php'); echo "用户名或者密码错误,3秒后跳转到登录页面"; } } } //点击教师登录按钮 elseif (isset($_POST['login_teacher'])) { $username = trim($_POST['username']); $password = trim($_POST['password']); if (($username == '') || ($password == '')) { header('refresh: 3; url=index.php'); echo "该用户名或者密码不能为空,3秒后跳转到登录页面"; exit; } else { $sql = "select * from teacher where username='$username'"; $res = mysqli_query($con, $sql); $n = mysqli_num_rows($res); if ($n > 0) { $row = mysqli_fetch_assoc($res); $pwd = $row['password']; //用户名或密码错误 if ($password != $pwd) { # code... header('refresh: 3; url=index.php'); echo "用户名或者密码错误,3秒后跳转到登录页面"; } else { //登录成功,将用户信息保存到session中 $_SESSION['username'] = $username; $_SESSION['islogin'] = 1; //用户信息保存到Cookie ,1天 setcookie("username", $username, time() + 24 * 60 * 60); setcookie( "pw", md5($username . md5($password)), time() + 24 * 60 * 60 ); //跳转到显示页面 header("location:show_teacher.php"); } } else { header('refresh: 3; url=index.php'); echo "用户名或者密码错误,3秒后跳转到登录页面"; } } } ?> </fieldset> </form> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> a:link { text-decoration: none; color: blue; } a:visited { color: blue; text-decoration: none; } </style> </head> <body> <?php header("Content-Type:text/html;charset=utf-8"); session_start(); //清除session $username = $_SESSION['username']; $_SESSION = array(); session_destroy(); //清除cookie setcookie("username", '', time() - 1); setcookie("code", '', time() - 1); echo "<a href='index.php'>点击重新登录</a>"; header('refresh: 5; url=index.php'); echo "<br />5秒钟后自动返回到主页"; ?> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0px; padding: auto; } a:link { text-decoration: none; color: blue; } a:visited { color: blue; text-decoration: none; } #box { margin: auto; margin-top: 200px; width: 800px; text-align: center; } table { width: 700px; padding: 0; margin: 0 auto; } td { border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7; font-size: 11px; padding: 6px 6px 6px 12px; color: #4f6b72; } tr:hover { background-color: #B0C4DE; } th { font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif; color: #4f6b72; border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7; border-top: 1px solid #C1DAD7; letter-spacing: 2px; text-transform: uppercase; text-align: center; padding: 6px 6px 6px 12px; background: #CAE8EA no-repeat; } </style> </head> <body> <div id="box"> <?php header("Content-Type:text/html;charset=utf-8"); session_start(); //首先判断Cookie是否有记住用户信息 if (isset($_COOKIE['username'])) { $_SESSION['username'] = $_COOKIE['username']; $_SESSION['islogin'] = 1; } if (isset($_SESSION['islogin'])) { //已经登录 echo "成绩查询中心!<br/><br/>你的学号:" . $_SESSION['username'] . " "; echo "<a href='logout.php'>注销</a>"; } else { //为登录 echo "你还未登录,请<a href='index.php'>登录</a>"; } ?> <table> <thead> <th>语文</th> <th>数学</th> <th>英语</th> <th>综合</th> </thead> <?php $db = new mysqli("localhost", "root", "", "resultsquerysystem"); $sql = "select * from score where username = '" . $_SESSION['username'] . "'"; $r = $db->query($sql); //传值 while ($attr = $r->fetch_row()) { echo " <tr> <td>{$attr[1]}</td> <td>{$attr[2]}</td> <td>{$attr[3]}</td> <td>{$attr[4]}</td> </tr>"; } ?> </table> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> a:link { text-decoration: none; color: blue; } a:visited { color: blue; text-decoration: none; } * { margin: 0px; padding: auto; } #box { margin: auto; margin-top: 200px; width: 800px; text-align: center; } table { width: 700px; padding: 0; margin: 20px auto; } td { border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7; font-size: 11px; padding: 6px 6px 6px 12px; color: #4f6b72; } tr:hover { background-color: #B0C4DE; } th { font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif; color: #4f6b72; border-right: 1px solid #C1DAD7; border-bottom: 1px solid #C1DAD7; border-top: 1px solid #C1DAD7; letter-spacing: 2px; text-transform: uppercase; text-align: center; padding: 6px 6px 6px 12px; background: #CAE8EA no-repeat; } .login_btn { border: none; background: #01A4F1; color: #fff; font-size: 14px; font-weight: bold; height: 28px; line-height: 28px; padding: 0 10px; cursor: pointer; } </style> </head> <body> <div id="box"> <?php header("Content-Type:text/html;charset=utf-8"); session_start(); //首先判断Cookie是否有记住用户信息 if (isset($_COOKIE['username'])) { $_SESSION['username'] = $_COOKIE['username']; $_SESSION['islogin'] = 1; } if (isset($_SESSION['islogin'])) { //已经登录 echo "成绩查询中心!<br/><br/>工号:" . $_SESSION['username'] . " "; echo "<a href='logout.php'>注销</a>"; } else { //为登录 echo "你还未登录,请<a href='index.php'>登录</a>"; } ?> <form method="post" action="delete_score.php"> <table> <thead> <th>学号</th> <th>语文</th> <th>数学</th> <th>英语</th> <th>综合</th> <th>操作</th> </thead> <?php $db = new mysqli("localhost", "root", "", "resultsquerysystem"); $sql = "select * from score"; $r = $db->query($sql); //传值 while ($attr = $r->fetch_row()) { echo " <tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td>{$attr[2]}</td> <td>{$attr[3]}</td> <td>{$attr[4]}</td> <td> <a href='alter_score.php?username=$attr[0]&yuwen=$attr[1]&shuxue=$attr[2]&yingyu=$attr[3]&zonghe=$attr[4]'>修改</a> <a href='delete_score.php?id=$attr[0]'>删除</a> </td> </tr>"; } ?> </table> </form> <a href="add_student.php"><button>添加学生信息</button></a> <a href="add_score.php"><button>添加学生成绩</button></a> </div> </body> </html>
推薦學習:《PHP影片教學》
以上是php程式碼怎麼實現成績查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。
