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中文网其他相关文章!

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。