学生信息管理系统简单案例

总共分四个文件:
conn.php:公用连接数据库文件
list.php:学生信息列表管理主页
add.php:实现学生信息添加功能
delete.php:实现学生信息删除功能
edit.php:实现学生信息修改功能
conn.php:

$db_host='localhost';
$db_user='root';
$db_pass='666666';
$db_name='phphx';
$charset='utf8';
@$link=mysqli_connect($db_host,$db_user,$db_pass);
if(!$link){
    echo 'mysql服务器连接失败';
    die();
}
if(!mysqli_select_db($link,$db_name)){
    echo "选择数据库{$db_name}失败!";
    die();
}
mysqli_set_charset($link,$charset);

list.php:

<?php 
//包含连接数据库的公共文件
require_once('./conn.php');
$sql='select * from student order by id desc';
$result=mysqli_query($link,$sql);
//获取多行记录
$arrs=mysqli_fetch_all($result,MYSQLI_ASSOC);
//获取记录数
$records=mysqli_num_rows($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生信息管理中心</title>
    <style>
        .title{
            text-align: center;
            margin-bottom:10px;
        }
        table{
            border:1px solid gray;
            width: 600px;
            text-align: center;
            border-collapse: collapse;
            margin:0px auto;
        }
        td,th{
            border:1px solid gray;
        }
    </style>
    <script type="text/javascript">
        //自定义js弹框,在用户点击确定时跳转网页
        function confirmDel(id){
            if(window.confirm('你真的要删除吗?')){
                location.href='./delete.php?id='+id;
            }
        }
    </script>
</head>
<body>
    <div class='title'>
    <h2>学生信息管理中心</h2>
    <a href="./add.php">添加学生</a>
    共有<font color='red'><?php echo $records; ?></font>个学生
    </div>
    <table>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>学历</th>
            <th>工资</th>
            <th>奖金</th>
            <th>籍贯</th>
            <th>操作选项</th>
        </tr>
        <?php  
        foreach($arrs as $arr){ 
        ?>
        <tr>
            <td><?php echo $arr['id']; ?></td>
            <td><?php echo $arr['name']; ?></td>
            <td><?php echo $arr['sex']; ?></td>
            <td><?php echo $arr['age']; ?></td>
            <td><?php echo $arr['edu']; ?></td>
            <td><?php echo $arr['salary']; ?></td>
            <td><?php echo $arr['bonus']; ?></td>
            <td><?php echo $arr['city']; ?></td>
            <td>
                <a href="./edit.php?id=<?php echo $arr['id'] ?>">修改</a>
                <a href="#" onClick=confirmDel(<?php echo $arr['id']?>)>删除</a>
            </td>
        </tr>
        <?php } ?>
    </table>
</body>
</html>
图片.png

add.php:

<?php 
//包含连接数据库的公共文件
require_once('./conn.php');
//判断表单是否合法提交
if(isset($_POST['token']) && $_POST['token']=='add'){   
    //获取表单提交数据
    $name   = $_POST['name'];
    $sex    = $_POST['sex'];
    $age    = $_POST['age'];
    $edu    = $_POST['edu'];
    $salary = $_POST['salary'];
    $bonus  = $_POST['bonus'];
    $city   = $_POST['city'];
    //构建插入的sql语句
    $sql="insert into student values(null,'$name','$sex',$age,'$edu',$salary,$bonus,'$city')";
    //判断数据是否插入成功    
    if(mysqli_query($link,$sql)){
        echo "<h2>学生信息添加成功!</h2>";
        header("refresh:3;url=list.php");
        die();
    }else{
        echo "<h2>学生信息添加失败!</h2>";
        header("refresh:3;url=list.php");
        die();
    }
}
 ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生信息管理中心</title>
    <style>
        .title{
            text-align: center;
            margin-bottom:10px;
        }
        table{
            border:1px solid gray;
            width: 600px;
            border-collapse: collapse;
            margin:0px auto;
        }
        td{
            border:1px solid gray;
        }
        .td_left{           
            text-align: right;
        }
    </style>
</head>
<body>
    <div class='title'>
    <h2>学生信息管理中心</h2>
    <a href="./list.php">返回首页</a>
    </div>
    <form action="" name="form1" method="post">
        <table>
            <tr>
                <td class='td_left'>姓名:</td>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <td class='td_left'>性别:</td>
                <td>
                    <input type="radio" name="sex" value='1' checked><input type="radio" name="sex" value='2'></td>
            </tr>
            <tr>
                <td class='td_left'>年龄:</td>
                <td><input type="text" name="age"></td>
            </tr>
            <tr>
                <td class='td_left'>学历:</td>
                <td>
                    <select name="edu">
                        <option value="1">初中</option>
                        <option value="2">高中</option>
                        <option value="3" selected>大专</option>
                        <option value="4">大本</option>
                        <option value="5">研究生</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class='td_left'>工资:</td>
                <td><input type="text" name="salary"></td>
            </tr>
            <tr>
                <td class='td_left'>奖金:</td>
                <td><input type="text" name="bonus"></td>
            </tr>
            <tr>
                <td class='td_left'>籍贯:</td>
                <td><input type="text" name="city"></td>
            </tr>
            <tr>
                <td class='td_left'></td>
                <td>
                    <input type="submit" value="提交">
                    <input type="hidden" name='token' value='add'>
                    <input type="reset" value="重置">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
图片.png

delete.php:

require_once('./conn.php');
//获取地址栏传递的id值
$id=$_GET['id'];
//构建删除的sql语句
$sql="delete from student where id={$id}";
//判断记录是否删除成功
if(mysqli_query($link,$sql)){
    echo "<h2>id={$id}的记录删除成功!</h2>";
    header("refresh:3;url=list.php");
    die();
}else{
    echo "<h2>id={$id}的记录删除失败!</h2>";
    header("refresh:3;url=list.php");
    die();
}
图片.png

edit.php:

<?php 
//包含连接数据库的公共文件
require_once('./conn.php');
$id=$_GET['id'];
$result=mysqli_query($link,"select * from student where id={$id}");
$arr=mysqli_fetch_assoc($result);
//判断表单是否合法提交
//为保证数据安全,采用令牌验证
//在此先用静态数据演示
if(isset($_POST['token']) && $_POST['token']=='add'){   
    //获取表单提交数据
    $name   = $_POST['name'];
    $sex    = $_POST['sex'];
    $age    = $_POST['age'];
    $edu    = $_POST['edu'];
    $salary = $_POST['salary'];
    $bonus  = $_POST['bonus'];
    $city   = $_POST['city'];
    //构建更新的sql语句
    $sql="update student set name='{$name}',sex='{$sex}',age={$age},edu='{$edu}',salary={$salary},bonus={$bonus},city='{$city}' where id=$id";
    //判断数据是否插入成功    
    if(mysqli_query($link,$sql)){
        echo "<h2>学生信息修改成功!</h2>";
        header("refresh:3;url=list.php");
        die();
    }else{
        echo "<h2>学生信息修改失败!</h2>";
        header("refresh:3;url=list.php");
        die();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生信息管理系统</title>
    <style>
        .title{
            text-align: center;
            margin-bottom:10px;
        }
        table{
            border:1px solid gray;
            width: 600px;
            border-collapse: collapse;
            margin:0px auto;
        }
        td{
            border:1px solid gray;
        }
        .td_left{           
            text-align: right;
        }
    </style>
</head>
<body>
    <div class='title'>
    <h2>学生信息管理中心</h2>
    <a href="./list.php">返回首页</a>
    </div>
    <form action="" name="form1" method="post">
        <table>
            <tr>
                <td class='td_left'>姓名:</td>
                <td><input type="text" name="name" value="<?php echo $arr['name']; ?>"></td>
            </tr>
            <tr>
                <td class='td_left'>性别:</td>
                <td>
                    <input type="radio" name="sex" value='1' 
                        <?php if($arr['sex']=='男'){echo 'checked';} ?>
                    >男
                    <input type="radio" name="sex" value='2'
                        <?php if($arr['sex']=='女'){echo 'checked';} ?>
                    >女
                </td>
            </tr>
            <tr>
                <td class='td_left'>年龄:</td>
                <td><input type="text" name="age" value="<?php echo $arr['age']; ?>"></td>
            </tr>
            <tr>
                <td class='td_left'>学历:</td>
                <td>
                    <select name="edu">
                        <option value="1" 
                            <?php if($arr['edu']=='初中'){echo 'selected';} ?>
                        >初中</option>
                        <option value="2"
                            <?php if($arr['edu']=='高中'){echo 'selected';} ?>
                        >高中</option>
                        <option value="3" 
                            <?php if($arr['edu']=='大专'){echo 'selected';} ?>
                        >大专</option>
                        <option value="4"
                            <?php if($arr['edu']=='大本'){echo 'selected';} ?>
                        >大本</option>
                        <option value="5"
                            <?php if($arr['edu']=='研究生'){echo 'selected';} ?>
                        >研究生</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class='td_left'>工资:</td>
                <td><input type="text" name="salary" 
                    value="<?php echo $arr['salary']; ?>"
                    ></td>
            </tr>
            <tr>
                <td class='td_left'>奖金:</td>
                <td><input type="text" name="bonus"
                    value="<?php echo $arr['bonus']; ?>"
                    ></td>
            </tr>
            <tr>
                <td class='td_left'>籍贯:</td>
                <td><input type="text" name="city"
                    value="<?php echo $arr['city']; ?>"
                    ></td>
            </tr>
            <tr>
                <td class='td_left'></td>
                <td>
                    <input type="submit" value="提交">
                    <input type="hidden" name='token' value='add'>
                    <input type="reset" value="重置">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
图片.png

猜你喜欢

转载自blog.csdn.net/csdn_heshangzhou/article/details/80878779