php+script实现简单的学生管理系统

学了php后写了一个简单的学生管理系统,因为就是一个简单的练习样式都没怎么写。

数据库用的是MySQL,用pdo链接的数据库。

数据库的格式为:

student
id int  
name varchar 50
sex varchar 2
age tinyint  
class varchar 50

php文件一共6个。分别是

1、index.php主要是罗列学生的信息

<div>
    include_once('menu.php');?>
    <table border="1" align="center" width=90%>
        <caption><h1>浏览学生信息h1>caption>
        <tr bgcolor="#cccccc">
            <th>IDth><th>姓名th><th>性别th><th>年龄th><th>班级th><th>操作th>
        tr>
                //链接数据库
        try{
            $pdo = new PDO("mysql:dbname=mydatabase;host=localhost","root","");
        }catch(PDOException $e){
            die("数据库链接失败".$e->getMessage());
        }
        //查询信息
        $sql = "select * from student";
        $result = $pdo->query($sql);
        foreach($result as $row){//循环罗列学生的信息
            echo '';
            echo "{$row['id']}";
            echo "{$row['name']}";
            echo "{$row['sex']}";
            echo "{$row['age']}";
            echo "{$row['class']}";
            echo "
                   ]})'>删除
                   ]}')'>修改
                ";
            echo '';
        }
        ?>
       table>
div>
<script>
    function doDel(id) {//当用户要删除信息的时候,弹出框,同时向删除也传递一个参数
        if (confirm("确定要删除吗?")) {
            window.location = 'action.php?action=del&id=' + id;
        }
    }
script>
2、menu.php主要是头部的一些操作菜单和一些简单的样式

<style>
    div{
        text-align: center;
    }
    #search{
        float: right;
    }
style>
<body>
<h2>学生信息管理h2>
<a href="index.php">浏览学生信息a>
<a href="add.php">增加学生信息a>
<form action="search.php?_=1" method="post" id="search">
    <input type="text" name="search">
    <button type="submit">查找button>
form>
<hr>
body>
3、action.php主要是对数据库的增、删、改。


//链接数据库
try{
    $pdo = new PDO("mysql:dbname=mydatabase;host=localhost","root","");
}catch(PDOException $e){
    die("数据库链接失败".$e->getMessage());
}

//通过action来操作
switch($_GET['action']){
    case "add"://为添加操作
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $age = $_POST['age'];
        $class = $_POST['class'];
        $sql1 = "insert into student values(null,'$name','$sex','$age','$class')";
        $res1 = $pdo->exec($sql1);
        if($res1){
            echo "";
        }else{
            echo "";
        }
    break;

    case "del"://删除操作
        $id = $_GET['id'];
        $sql2 = "delete from student where id = {$id}";
        $pdo->exec($sql2);
        header('location:index.php');
        //echo会闪
       // echo "";
    break;
    case "edit":
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $age = $_POST['age'];
        $class = $_POST['class'];
        $id = $_POST['id'];
        $sql3 = "update student set name = '{$name}',sex = '{$sex}',age = {$age},class={$class} where id = {$id}";
        $res3 = $pdo->exec($sql3);
        if($res3>0){
            echo "";
        }else{
            echo "";
        }
break;
}
4、add.php主要是添加学生信息

include_once('menu.php');?>
    <h3>添加学生信息h3>

    <form action="action.php?action=add" method="post">
        <label for="stuName">姓名:label>
        <input type="text" name="name" id="stuName"><br><br>

        性别:<input type="radio" name="sex" value="m" checked>        <input type="radio" name="sex" value="w"><br><br>

        <label for="stuAge">年龄:label>
        <input type="text" name="age" id="stuAge"><br><br>

        <label for="stuClass">班级:label>
        <input type="text" name="class" id="stuClass"><br><br>
        <input type="submit" value="添加">
        <input type="reset" value="重置">
    form>
div>
5、edit.php修改学生信息

<div>
include_once('menu.php');
//链接数据库
try{
    $pdo = new PDO("mysql:dbname=mydatabase;host=localhost","root","");
}catch(PDOException $e){
    die("数据库链接失败".$e->getMessage());
}
//拼装sql语句,来去信息然后放到标签里
$sql = "select * from student where id=".$_GET['id'];
$stmt = $pdo->query($sql);
if($stmt->rowCount() > 0){
    $list = $stmt->fetch(PDO::FETCH_ASSOC);  //解析数据
}else{
    die("没要有修改的数据");
}

?>
<h3>修改学生信息h3>

<form action="action.php?action=edit" method="post">
    <input type="hidden" name="id" value="$list['id'];?>">
    <label for="stuName">姓名:label>
    <input type="text" name="name" id="stuName" value="$list['name'] ?>"><br><br>

    性别:<input type="radio" name="sex" value="m" ($list['sex']=='m') ? 'checked' : ''?>>    <input type="radio" name="sex" value="w" ($list['sex']=='w') ? 'checked' : ''?>><br><br>

    <label for="stuAge">年龄:label>
    <input type="text" name="age" id="stuAge" value="$list['age'] ?>"><br><br>

    <label for="stuClass">班级:label>
    <input type="text" name="class" id="stuClass" value="$list['class']?>"><br><br>
    <input type="submit" value="修改">
    <input type="reset" value="重置">
form>
div>
6、search.php主要是当在input输入框查找特定的学生信息的时候跳转的网页

<script>
    function doDel(id) {
        if (confirm("确定要删除吗?")) {
            window.location = 'action.php?action=del&id=' + id;//中间不能有空格不然会失败
        }
    }
script>
<div>
    include_once('menu.php'); ?>
    <table border="1" align="center" width=90%>
        <caption><h1>浏览学生信息h1>caption>
        <tr bgcolor="#cccccc">
            <th>IDth>
            <th>姓名th>
            <th>性别th>
            <th>年龄th>
            <th>班级th>
            <th>操作th>
        tr>
                //链接数据库
        try {
            $pdo = new PDO("mysql:dbname=mydatabase;host=localhost", "root", "");
        } catch (PDOException $e) {
            die("数据库链接失败" . $e->getMessage());
        }
        $search = $_POST['search'];
        $sql = "select * from student where name = '{$search}'";
        $result = $pdo->query($sql);

        foreach ($result as $row) {
            echo '';
            echo "{$row['id']}";
            echo "{$row['name']}";
            echo "{$row['sex']}";
            echo "{$row['age']}";
            echo "{$row['class']}";
            echo "
                   ]})'>删除
                   ]}')'>修改
                ";
            echo '';
        }
        ?>
    table>
div>


你可能感兴趣的:(JavaScript)