分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

学生管理项目PHP

发布时间:2023-09-06 02:19责任编辑:赖小花关键词:PHP
menu.php<html><head> ???<meta charset="UTF-8"> ???</head><h2>学生信息管理</h2><a href="index.php">浏览学生</a><a href="add.php">增加学生</a><hr/></html>
index.php<html><head> ???<meta charset="UTF-8"> ???<title>学生信息管理</title><script>function doDel(id){ ???if(confirm("确定要删除吗?")){ ???window.location = ‘action.php?action=del&id=‘+id; ???}}</script></head><body><center><?phpinclude("menu.php");?><h3>浏览学生信息</h3> ???<table width = "600" border = "1" > ???<tr><th>ID</th><th>姓名</th><th>年龄</th><th>操作</th></tr><?phptry{ ???#连接数据库 ???$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");}catch(PDOException $e){die("数据库连接失败".$e->getMessage());}#print_r($pdo);##执行sql语句$sql = "select * from test";foreach($pdo->query($sql) as $row){ ???echo "<tr>"; ???echo "<td>{$row[‘id‘]}</td>"; ???echo "<td>{$row[‘name‘]}</td>"; ???echo "<td>{$row[‘age‘]}</td>"; ???echo "<td>{$row[‘sex‘]}</td>"; ???echo "<td> ???????<a href=‘javascript:doDel({$row[‘id‘]})‘>删除</a> ???????<a href=‘edit.php?id={$row[‘id‘]}‘>修改</a> ???????</td>"; ???echo "</tr>";}?> ???</table></center></body></html>
edit.php<!DOCTYPE html><html><head> ???<meta charset = "UTF-8"> ???<title>修改学生信息</title></head><body><center><?phpinclude("menu.php");#连接数据库try{$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");}catch(PDOException $e){ ???die("数据库连接失败".$e->getMessage());}#拼成sql语句,查询信息 ???$sql = "select * from test where id = ".$_GET[‘id‘];$stmt = $pdo->query($sql);#var_dump($stmt);if($stmt->rowCount() > 0){ ???$arr = $stmt->fetch(PDO::FETCH_ASSOC); ???# ???die("没有需要修改的数据");# ???var_dump($arr);}else{ ???die("没有需要修改的数据");}?><form action="action.php?action=edit" method = "post"><input type="hidden" name="id" value="<?php echo $arr[‘id‘]; ?>" /> ???<table><tr><td>姓名</td><td><input type = "text" name = "name" value="<?php echo $arr[‘name‘];?>" ></td><td></td></tr><tr><td>年龄</td><td><input type = "text" name = "age" value= <?php echo $arr[‘age‘];?>></td></tr><tr><td>姓名</td><td><input type = "radio" name = "sex" value = ‘男‘ <?php echo ($arr[‘sex‘]) == ‘男‘ ? "checked": "" ?> > 男&nbsp;<input type = "radio" name = "sex" value = ‘女‘ <?php echo ($arr[‘sex‘]) == ‘女‘ ? "checked" : ""?>> 女</td><td></td></tr><tr><td>&nbsp;</td><td><input type = "submit" ?value = "修改"/><input type = "reset" ?value = "重置"/></td></tr></table></form></center></body></html>
add.php<html><head><title>学生管理信息</title></head><body><center><?phpinclude("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 = "text" name="age" /></td></tr><tr><td>性别</td><td><input type = "radio" name="sex" value = "男" /> 男<input type = "radio" name="sex" value = "女" /> 女</td></tr><tr><td>&nbsp;</td><td><input type = "submit" value = "增加" /> <input type = "reset" value = "重置" /> </td></tr></table></form></center></body></html>
action.php<html><head> ???<meta charset=“UTF-8"> ???</head><body><?php#1.连接数据库try{ ???$pdo = new PDO("mysql:host=localhost;dbname=php","root","root");}catch(PDOException $e){ ???die("数据库连接失败".$e->getMessage());}#2.通过aaction的值判断相应的操作switch($_GET[‘action‘]){case ‘add‘://添加的操作 ???$name = $_POST[‘name‘]; ???$age = $_POST[‘age‘]; ???$sex = $_POST[‘sex‘]; ???????$sql = "insert into test value(null,‘{$name}‘,‘{$age}‘,‘{$sex}‘)"; ???$rw = $pdo->exec($sql); ???if($rw > 0) ???{ ???????echo "<script>alert(‘SUCCESS‘);window.location=‘index.php‘</script>"; ???} ???else ???{ ???????echo "<script>alert(‘FAIL‘);window.history.back();</script>"; ???} ???break;case ‘del‘: ???$id = $_GET[‘id‘]; ???$sql = "delete from test where id = {$id}"; ???$pdo->exec($sql); ???header("location:index.php"); ???break;case ‘edit‘: ???????$id = $_POST[‘id‘]; ???????$name = $_POST[‘name‘]; ???????$age = $_POST[‘age‘]; ???????$sex = $_POST[‘sex‘]; ???????$sql = "update test set name = ‘{$name}‘,age = {$age},sex = ‘{$sex}‘ where id = {$id}"; ???????$res = $pdo->exec($sql); ???????#echo $res; ???????if($res > 0) ???????{ ???????????echo "<script>alert(‘SUCCESS‘);window.location=‘index.php‘</script>"; ???????} ???????else ???????{ ???????????echo "<script>alert(‘FAIL‘);window.history.back();</script>"; ???????}break; ???}?></body></html>

学生管理项目PHP

原文地址:https://www.cnblogs.com/qinzhihaovip/p/9837166.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved