-- Database: test

-- 表的结构 message

CREATE TABLE `message` (
`id` tinyint(1) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`sex` varchar(50) NOT NULL,
`age` tinyint(1) NOT NULL,
`classid` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

index.php

<!DOCTYPE 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>
<?php
include_once "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.连接数据库
try {
$pdo = new PDO("mysql:host=localhost;dbname=test;", "root", "");
} catch (PDOException $e) {
die("数据库连接失败" . $e->getMessage());
}
//2.解决中文乱码问题
$pdo->query("SET NAMES 'UTF8'");
//3.执行sql语句,并实现解析和遍历
$sql = "SELECT * FROM message ";
foreach ($pdo->query($sql) as $row) {
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['sex']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td>{$row['classid']}</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>

menu.php

<!DOCTYPE html>
<html lang="en">
<body>
<h2>学生管理系统</h2>
<a href="index.php"> 浏览学生</a>
<a href="add.php"> 添加学生</a>
<hr>
</body>
</html>

add.php

<html>
<head>
<title>学生信息管理</title>
</head>
<body>
<center>
<?php include("menu.php"); ?>
<h3>增加学生信息</h3>
<form method="post" action="action.php?action=add"> <table>
<tr>
<td>姓名</td>
<td><input name="name" type="text"/></td> </tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="男"/> 男
<input type="radio" name="sex" value="女"/> 女
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>班级</td>
<td><input name="classid" type="text"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="增加"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table> </form>
</center>
</body>
</html>

edit.php

<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title> </head>
<body>
<center>
<?php
include_once "menu.php";
//1.连接数据库
try {
$pdo = new PDO("mysql:host=localhost;dbname=test;", "root", "");
} catch (PDOException $e) {
die("数据库连接失败" . $e->getMessage());
}
//2.防止中文乱码
$pdo->query("SET NAMES 'UTF8'");
//3.拼接sql语句,取出信息
$sql = "SELECT * FROM message WHERE id =" . $_GET['id'];
$stmt = $pdo->query($sql);//返回预处理对象
if ($stmt->rowCount() > 0) {
$stu = $stmt->fetch(PDO::FETCH_ASSOC);//按照关联数组进行解析
} else {
die("没有要修改的数据!");
}
?>
<form method="post" action="action.php?action=edit"> <input type="hidden" name="id" id="id" value="<?php echo $stu['id']; ?>"/>
<table>
<tr>
<td>姓名</td>
<td><input id="name" name="name" type="text" value="<?php echo $stu['name'] ?>"/></td> </tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="男" <?php echo ($stu['sex'] == "男") ? "checked" : "" ?>/> 男
<input type="radio" name="sex" value="女" <?php echo ($stu['sex'] == "女") ? "checked" : "" ?>/> 女
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" id="age" value="<?php echo $stu['age'] ?>"/></td>
</tr>
<tr>
<td>班级</td>
<td><input id="classid" name="classid" type="text" value="<?php echo $stu['classid'] ?>"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="修改"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table> </form> </center>
</body>
</html>

action.php

<?php

//1.连接数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=test;","root","");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
} //2.通过action的值做地应操作 switch($_GET['action']){
case "add"://增加操作
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$classid = $_POST['classid']; $sql = "insert into message values(null,'{$name}','{$sex}','{$age}','{$classid}')";
$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 message where id={$id}";
$pdo->exec($sql);
header("Location:index.php");
break; case "edit": //1.获取表单信息
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$classid = $_POST['classid'];
$id = $_POST['id']; $sql = "update message set name='{$name}',sex='{$sex}',age={$age},classid={$classid} 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;
}
?>

PHP案例:学生信息管理系统的更多相关文章

  1. 面向对象案例-学生信息管理系统V0.6

    更新版本 面向对象案例 - 学生信息管理系统V1.0 项目要求: 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的方法 1. ...

  2. 面向对象案例 - 学生信息管理系统V1.0

    学生管理系统项目[所有知识点整合] 1. 学生管理系统项目 尝试完成以下功能 实体类: 学生类: id, 姓名,年龄,性别,成绩 需要使用数组保存学生信息 Student[] allStu 需要完成的 ...

  3. 面向对象案例-学生信息管理系统V1.1

    1.学生类 package com.qfedu.student.entity; /** * 学生类实体 * * @author GGGXXC * */ public class Student { p ...

  4. Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...

  5. Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...

  6. Py学生信息管理系统 案例(优化版)

    # 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...

  7. Python基础案例练习:制作学生信息管理系统

    一.前言 学生信息管理系统,相信大家或多或少都有做过 最近看很多学生作业都是制作一个学生信息管理系统 于是,今天带大家做一个简单的学生信息管理系统 二.开发环境: 我用到的开发环境 Python 3. ...

  8. 基于数据库MySQL的简易学生信息管理系统

    通过这几天学习Mysql数据库,对其也有了基本的了解,为了加深印象,于是就写了一个最简易的学生信息管理系统. 一:基本要求 1.通过已知用户名和密码进行登录: 2.可以显示菜单: 3.可以随时插入学生 ...

  9. C++ 简单的学生信息管理系统

    // // main.cpp // 2013-7-17作业1 // // Created by 丁小未 on 13-7-17. // Copyright (c) 2013年 dingxiaowei. ...

随机推荐

  1. 基于 Scrapy-redis 的分布式爬虫详细设计

    基于 Scrapy-redis 的分布式爬虫设计   目录 前言 安装 环境 Debian / Ubuntu / Deepin 下安装 Windows 下安装 基本使用 初始化项目 创建爬虫 运行爬虫 ...

  2. yaha分词

    yaha分词:https://github.com/jannson/yaha

  3. Ftp上传文件

    package net.util.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

  4. .net framework中重新注册IIS

    要为 ASP.NET 修复 IIS 映射,请按照下列步骤执行操作:运行 Aspnet_regiis.exe 实用工具:单击“开始”,然后单击“运行”.在“打开”文本框中,键入 cmd,然后按 ENTE ...

  5. 绘制函数y=(x^2-2x+1)/(x^2+x+2)的曲线

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  6. 超酷的实时颜色数据跟踪javascript类库 - Tracking.js

    来源:GBin1.com 今天介绍这款超棒的Javascript类库是 - Tracking.js,它能够独立不依赖第三方类库帮助开发人员动态跟踪摄像头输出相关数据. 这些数据包括了颜色或者是人, 这 ...

  7. 常用HTML标签的全称及描述

    常用HTML标签的英文全称及简单描述   HTML标签 英文全称 中文释义 a Anchor 锚 abbr Abbreviation 缩写词 acronym Acronym 取首字母的缩写词 addr ...

  8. profile_oracle设置某用户password永只是期

    原创作品.出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46888139 or ...

  9. Android 底部TabActivity(1)——FragmentActivity

    先看看效果图: 第一篇Tab系列的文章首先实现这样的风格的底部Tab:背景条颜色不变,我们是用了深灰的颜色,图标会发生对应的变化.当选中某个标签后该标签的背板会由正常的颜色变为不正常,哈哈,是变为加深 ...

  10. Python—— 性能分析入门指南

    虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题. 分析程序的性能可以归结为回答四个基本问题: ...