【web前端开发】学生信息管理系统

期末作业


模板网址:https://shenruiqing.cn/stu/add.php
在这里插入图片描述
在这里插入图片描述

原计划


不看讲解视频,自己写出来

代码

main.css

.text0{
    
    
    font-size:25px;
    font-weight:bolder;

}
.text1{
    
    
    font-size: 15px;
}
.text2{
    
    
    font-size: 15px;
}
.text3{
    
    
    font-size: 18px;
    font-weight:bold;
}

main.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息主页面</title>
    <style>
        
    </style>
</head>
    <link rel="stylesheet" href="main.css">
<body>
    <center>
        <!-- 1.title -->
        <p class="text0">学生信息管理系统</p>
        <!-- #1.跳转别的自己编写的页面way -->
        <a href="main.php" class="text1">浏览信息</a>
        <a href="insert.php" class="text1">添加信息</a>
        <!-- #2.水平线 -->
        <hr>
        <br>

        <p class="text3" >学生信息浏览</p>
        <!-- #3.input背景文字 -->
        <input placeholder="请输入查询信息"  ><button>查询</button>
        <br>

        <?php
        // print_r($_POST);

        // 输出
        if($_SERVER['REQUEST_METHOD'] === 'POST'){
    
    
            
            echo '</pre>';
            echo "<table border='1'>";
            echo '<tr>';
            echo '<th>学号</th>';
            echo '<th>姓名</th>';
            echo '<th>性别</th>';
            echo '<th>电话</th>';
            echo '<th>年龄</th>';
            echo '<th>学院</th>';
            echo '</tr>';

            //数组访问,中括号访问
            echo '<tr>';
            echo "<td>{
      
      $_POST['stu_no']}</td>";
            echo "<td>{
      
      $_POST['stu_name']}</td>";
            echo "<td>{
      
      $_POST['gender']}</td>";
            echo "<td>{
      
      $_POST['telephone']}</td>";
            echo "<td>{
      
      $_POST['age']}</td>";
            echo "<td>{
      
      $_POST['college']}</td>";
            echo '</tr>';
            
            echo "</table>";
        }
        ?>

    </center>

</body>
</html>

inset.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加信息页面</title>
</head>
    <link rel="stylesheet" href="main.css">
<body>
<body>
    <center>
        <!-- 1.title -->
        <p class="text0">学生信息管理系统</p>
        <!-- #1.跳转别的自己编写的页面way -->
        <a href="main.php" class="text1">浏览信息</a>
        <a href="insert.php" class="text1">添加信息</a>
        <!-- #2.水平线 -->
        <hr>
        <br>


        <form  method="post" action="main.php">
            <table>
                <tr>
                    <td>学号:</td>
                    <td><input type="text" name="stu_no"></td>
                </tr>
                <tr>
                    <td>姓名:</td>
                    <td><input type="text" name="stu_name" ></td>
                </tr>
                <tr>
                    <td>姓别:</td>
                    <td>
                        男:<input type="radio" name="gender" value="男">
                        女:<input type="radio" name="gender" value="女">
                    </td>
                </tr>
                <tr>
                    <td>电话:</td>
                    <td><input type="text" name="telephone"></td>
                </tr>
                <tr>
                    <td>年龄:</td>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td>学院:</td>
                    <td><input type="text" name="college"></td>
                </tr>

                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="submit" value="添加">
                        <input type="reset" name="reset" value="重置"><br>
                        <span class="text3">GET</span>
                    </td>
                </tr>
            </table>
        </form>
        
    </center>
</body>
</html>


<!-- 1.接受输入框的数据,并且传给MySQL 
1.1 在此页面输入框输入并且在此页面中打印 --ok
1.1.1 在main页面中打印 --ok
1.2 使用MySQL接收  -->


    <!-- else  -->
<!-- 1.GET怎么改POST1-->
<!-- 2.main表格边框改为蓝色 -->

效果图

在这里插入图片描述

在这里插入图片描述
可以添加数据,但是因为没有存储功能,会覆盖。

瓶颈

没学过,怎么把数据从MySQL中打印出来

看课


目的:不要死板的按着上面一步步做,看自己需要的,按自己的思路做,最后再回顾一遍,总结我的方法和他的差距

建树文件

在这里插入图片描述

使用遍历数组进行打印

遍历二维数组方法

文章:【web前端开发】后台PHP
https://blog.csdn.net/m0_65431212/article/details/126911190
在这里插入图片描述

新思路


1.使用MySQL进行存储
2.使用数组遍历进行打印

1.改main.php为conn.php,连接数据库

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息主页面</title>
    <style>
        
    </style>
</head>
    <link rel="stylesheet" href="main.css">
<body>
    <center>
        <!-- 1.title -->
        <p class="text0">学生信息管理系统</p>
        <!-- #1.跳转别的自己编写的页面way -->
        <a href="main.php" class="text1">浏览信息</a>
        <a href="insert.php" class="text1">添加信息</a>
        <!-- #2.水平线 -->
        <hr>
        <br>



        <p class="text3" >学生信息浏览</p>
        <!-- #3.input背景文字 -->
        <input placeholder="请输入查询信息"  ><button>查询</button>
        <br>



    <?php
    //配置变量
    $dbtype = "mysql";//设置数据库类型
    $host = "localhost";//设置主机名称
    $dbname = "mydemo";//设置默认数据库名称
    $username = "root";//设置数据库用户名
    $password = "";//设置数据库用户密码

    // //获取数据库
    // $pdo = new PDO("{$dbtype}:host={$host};dbname={$dbname}",$username,$password);
    // var_dump($pdo);
    try{
    
    
        //获取数据库连接
        $pdo = new PDO("{
      
      $dbtype}:host={
      
      $host};dbname={
      
      $dbname}",$username,$password);
        var_dump($pdo);
        echo "连接成功";
    }
    catch(PDOException $e){
    
    
        print "Error!: ". $e->getMessage() ."<br>";
        die();
    }
    ?>


    </center>

</body>
</html>

在这里插入图片描述

2.使用MySQL方法添加学生

在这里插入图片描述
由于使用conn.php为主页面,导致页面混乱

3.建立树文件

在这里插入图片描述

4.使页面上端不变

新知识


  1. '.css’文件以及common文件夹,应该和< link >对应的文件放在一个文件夹中,否则应该使用绝对路径引用
  2. < hr >水平线
  3. < a >标签的点击跳转
  4. php中require与include区别
  5. 公共文件不需要!+enter的html格式,直接开写
  6. < h1 >标题标签自带换行功能
  7. 输入框背景字placeholder
  8. 标题标签不能放在<?php?>中
<input placeholder="请输入查询信息"  >

9.表格属性详解
看这个文章

https://blog.csdn.net/TDCQZD/article/details/82226260?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166376330616782428699984%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=166376330616782428699984&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~hot_rank-3-82226260-null-null.142v49control,201v3add_ask&utm_term=%E8%A1%A8%E6%A0%BC%E6%A0%87%E7%AD%BE%E5%B1%9E%E6%80%A7&spm=1018.2226.3001.4187

  1. 两列归一列
<th colspan="2">操作</th>

在这里插入图片描述
11. 使用表格功能布局多个输入框情况
把boder设置为0
在这里插入图片描述
12. 第一次进入页面默认为get请求,如果自己设置了post,第一次进入会报错,解决方式:

if($_SERVER["REQUEST_METHOD"] === 'POST')
  1. 使用heder实现跳转页面
if($result > 0){
    
    
                header('Location:index.php');
            }
  1. php中< th >< a >组合报错
    在这里插入图片描述
    在这里插入图片描述
    原因:网址此处使用单引号则正确
    代码:
 echo "<td><a href='delete.php'>删除</a></td>";

答案


删除功能


新建delete.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php 
    require 'common/conn.php';

    $stu_get = $_GET['stu_no'];
    print_R($stu_get);

    $sql = "DELETE from stu where stu_no='{
      
      $stu_get}'";

    $result = $pdo->exec($sql);

    header('Location/index.php');
    
?>
</body>
</html>

点击删除,效果如下:
在这里插入图片描述

查询功能


普通查询

1.输入框name使用数据库功能

<?php
            $sql = "SELECT * from stu";
            // print_r($sql);

            if(isset($_GET['select'])){
    
    
                print_r(($_GET['select']));
            }

在这里插入图片描述

2.PHP的拼接技术,实现查询功能

在这里插入图片描述

分开作用:新赋值查询的条件$select
注意:where前面空一格!!!

代码改进:

$sql = "SELECT * from stu";
            // print_r($sql);

            if(isset($_GET['select'])){
    
    
                print_r(($_GET['select']));

                $select  =$_GET['select'];

                $sql .= " where stu_no='{
      
      $select}'";
            }

效果:
在这里插入图片描述

模糊查询

语法:

以“土木工程”为例:
“where 数据库字段 like ‘PHP变量%’”; //输入土木可以检索
“where 数据库字段 like ‘%PHP变量’”; //输入工程可以检索
“where 数据库字段 like ‘%PHP变量%’”; //输入什么都可以
一般:把两个%都写上

代码改进:

if(isset($_GET['select'])){
    
    
                print_r(($_GET['select']));

                $select  =$_GET['select'];

                $sql .= " where stu_no like '%$select%'";
            }

只输入4,效果:
在这里插入图片描述

修改功能


1.copy一下add.php,且改为一维数组

一维数组方法fetch
在这里插入图片描述
删去add.php抄过来的<?php?>内容,改写:

<?php
        include 'common\menu.php';
        require 'common\conn.php';

        $stu_get = $_GET['stu_no'];

        $sql = "SELECT * from stu where stu_no = '{
      
      $stu_get}'";

        $result = $pdo -> query($sql);

        $stu_arr = $result -> fetch(PDO::FETCH_ASSOC);
        print_r($stu_arr);
        
?>

在这里插入图片描述

2.获取数据库查询数据,且以表单展现给用户

在这里插入图片描述

改进代码:

<form action="" method="post">
            <table>
                <tr>
                    <td>学号:</td>
                    <td><input type="text" name="stu_no" value="<?php echo $stu_arr['stu_no'] ?>"></td>
                </tr>
                <tr>
                    <td>姓名:</td>
                    <td><input type="text" name="stu_name" value="<?php echo $stu_arr['stu_name'] ?>"></td>
                </tr>
                <tr>
                    <td>姓别:</td>
                    <td>
                        男:<input type="radio" name="gender" value="男" <?php 
                                                                            if($stu_arr['gender']=='男'){
    
    
                                                                                echo 'checked';
                                                                            }
                                                                        ?>>
                        女:<input type="radio" name="gender" value="女"<?php 
                                                                            if($stu_arr['gender']=='女'){
    
    
                                                                                echo 'checked';
                                                                            }
                                                                        ?>>
                    </td>
                </tr>
                <tr>
                    <td>电话:</td>
                    <td><input type="text" name="telephone" value="<?php echo $stu_arr['telephone'] ?>"></td>
                </tr>
                <tr>
                    <td>年龄:</td>
                    <td><input type="text" name="age" value="<?php echo $stu_arr['age'] ?>"></td>
                </tr>
                <tr>
                    <td>学院:</td>
                    <td><input type="text" name="college" value="<?php echo $stu_arr['college'] ?>"></td>
                </tr>

                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="submit" value="修改">
                        <input type="reset" name="reset" value="重置">
                    </td>
                </tr>
            </table>
        </form>

从index.php页面进入,数据显示正确:
在这里插入图片描述

3.表单打印输出数据

<?php
        print_r($_SERVER["REQUEST_METHOD"]);//返回当前浏览器的请求方式
        if($_SERVER["REQUEST_METHOD"] === 'POST'){
    
    
            print_r($_POST);

            $stu_no = $_POST['stu_no'];
            $stu_name = $_POST['stu_name'];
            $gender = $_POST['gender'];
            $telephone = $_POST['telephone'];
            $age = $_POST['age'];
            $college = $_POST['college'];
        }
        $sql = "UPDATE stu set stu_no='{
      
      $stu_no}',stu_name='{
      
      $stu_name}',
        gender='{
      
      $gender}',telephone='{
      
      $telephone}',age='{
      
      $age}',college='{
      
      $college}' 
        where stu_no='{
      
      $stu_get}'";

        $result = $pdo -> exec($sql);
        print_r($result);

        if($result > 0){
    
    
            header('Location:index.php');
        }else{
    
    
            echo '修改失败';
        }
        ?>

在这里插入图片描述

代码集合

conn.php


<!-- 连接数据库 -->
<?php
    //配置变量
    $dbtype = "mysql";//设置数据库类型
    $host = "localhost";//设置主机名称
    $dbname = "mydemo";//设置默认数据库名称
    $username = "root";//设置数据库用户名
    $password = "";//设置数据库用户密码
    
    //创建数据源
    $dsn = "{
      
      $dbtype}:host={
      
      $host};dbname={
      
      $dbname}";

    // //获取数据库
    // $pdo = new PDO("{$dbtype}:host={$host};dbname={$dbname}",$username,$password);
    // var_dump($pdo);


    try{
    
    
        //获取数据库连接
        $pdo=new PDO($dsn,$username,$password);
        $pdo->query("set names utf8");

        // echo "连接成功";
    }
    catch(PDOException $e){
    
    
        die('连接失败' . $e->getMessage());
    }
?>

menu.php


<!-- 公共导航栏 -->
<!-- 1.title -->
<!-- <link rel="stylesheet" href="1.css"> -->

<h2>学生信息管理系统</h2>

<a href='index.php'>浏览信息</a>
<a href='add.php'>添加信息</a>

<hr>

index.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息主页面</title>
</head>
    <link rel="stylesheet" href="1.css">
<body>
    <center>
        <?php
        // 1.公共上端
        include 'common\menu.php';
        require 'common\conn.php';
        ?>
        <!-- 2.本页简单元素 -->
        <br>
        <h3>学生信息浏览</h3>
        <!-- <input placeholder="请输入查询信息"  ><button>查询</button> -->
        

        <!-- 查询信息框 -->
        <form action="" method="get">
            <input type="text" name="select" 
            placeholder="请输入查询信息">
            <input type="submit" value="查询">
        </form>

        <!-- 3.打印表格 -->
        <table border="1" width="800" cewllspacing="0" 
        cellpadding="5" bordercolor="green" style="font-family:'楷体';">
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>电话</th>
                <th>年龄</th>
                <th>学院</th>
                <th colspan="2">操作</th>
            </tr>
            <!-- 4.返回数据库中学生数据的二维数组数据-->
            <?php
            $sql = "SELECT * from stu";
            // print_r($sql);

            if(isset($_GET['select'])){
    
    
                print_r(($_GET['select']));

                $select  =$_GET['select'];

                $sql .= " where stu_no like '%$select%'";
            }

            $result = $pdo->query($sql);
            // print_r($result);

            $stu_arr = $result->fetchALL(PDO::FETCH_ASSOC);
            // print_r($stu_arr);

            // 5.遍历二维数组并且打印

            foreach($stu_arr as $stu){
    
    
                echo "<tr>";
                echo "<td>{
      
      $stu['stu_no']}</td>";
                echo "<td>{
      
      $stu['stu_name']}</td>";
                echo "<td>{
      
      $stu['gender']}</td>";
                echo "<td>{
      
      $stu['telephone']}</td>";
                echo "<td>{
      
      $stu['age']}</td>";
                echo "<td>{
      
      $stu['college']}</td>";
                echo "<td><a href='updata.php?stu_no={
      
      $stu['stu_no']}'>修改</a></td>";
                echo "<td><a href='delete.php?stu_no={
      
      $stu['stu_no']}'>删除</a></td>";
                
                echo "</tr>";
            }
            ?>

        </table>


    </center>
</body>
</html>

<!-- 浏览页面 查找页面 -->

add.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加信息页面</title>
</head>
<body>
    <center>
        <?php
        include 'common\menu.php';
        require 'common\conn.php';
        ?>

        <br>
        <h3>添加学生信息</h3>

        <!-- 1.输入框设置 -->

        <form action="" method="post">
            <table>
                <tr>
                    <td>学号:</td>
                    <td><input type="text" name="stu_no"></td>
                </tr>
                <tr>
                    <td>姓名:</td>
                    <td><input type="text" name="stu_name"></td>
                </tr>
                <tr>
                    <td>姓别:</td>
                    <td>
                        男:<input type="radio" name="gender" value="男" check>
                        女:<input type="radio" name="gender" value="女">
                    </td>
                </tr>
                <tr>
                    <td>电话:</td>
                    <td><input type="text" name="telephone"></td>
                </tr>
                <tr>
                    <td>年龄:</td>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td>学院:</td>
                    <td><input type="text" name="college"></td>
                </tr>

                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="submit" value="添加">
                        <input type="reset" name="reset" value="重置">
                    </td>
                </tr>
            </table>
        </form>
        <?php
        
        // 2.数据库添加的组合代码

        print_r($_SERVER["REQUEST_METHOD"]);//返回当前浏览器的请求方式
        if($_SERVER["REQUEST_METHOD"] === 'POST'){
    
    
            print_r($_POST);
            $stu_no = $_POST['stu_no'];
            $stu_name = $_POST['stu_name'];
            $gender = $_POST['gender'];
            $telephone = $_POST['telephone'];
            $age = $_POST['age'];
            $college = $_POST['college'];

            $sql = "INSERT into stu(stu_no,stu_name,gender,telephone,
            age,college) values('{
      
      $stu_no}','{
      
      $stu_name}','{
      
      $gender}','{
      
      $telephone}'
            ,'{
      
      $age}','{
      
      $college}')";

            $result = $pdo -> exec($sql);
            print_r($result);

            if($result > 0){
    
    
                header('Location:index.php');
            }else{
    
    
                echo '添加失败';
            }
        }

        ?>   
    </center>
</body>
</html>

<!-- 添加页面 -->

updata.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改信息页面</title>
</head>
<body>
    <center>
        <?php
        include 'common\menu.php';
        require 'common\conn.php';

        $stu_get = $_GET['stu_no'];

        $sql = "SELECT * from stu where stu_no = '{
      
      $stu_get}'";

        $result = $pdo -> query($sql);

        $stu_arr = $result -> fetch(PDO::FETCH_ASSOC);
        // print_r($stu_arr);
        
        ?>

        <br>
        <h3>修改学生信息</h3>

        <!-- 1.输入框设置 -->

        <form action="" method="post">
            <table>
                <tr>
                    <td>学号:</td>
                    <td><input type="text" name="stu_no" value="<?php echo $stu_arr['stu_no'] ?>"></td>
                </tr>
                <tr>
                    <td>姓名:</td>
                    <td><input type="text" name="stu_name" value="<?php echo $stu_arr['stu_name'] ?>"></td>
                </tr>
                <tr>
                    <td>姓别:</td>
                    <td>
                        男:<input type="radio" name="gender" value="男" <?php 
                                                                            if($stu_arr['gender']=='男'){
    
    
                                                                                echo 'checked';
                                                                            }
                                                                        ?>>
                        女:<input type="radio" name="gender" value="女"<?php 
                                                                            if($stu_arr['gender']=='女'){
    
    
                                                                                echo 'checked';
                                                                            }
                                                                        ?>>
                    </td>
                </tr>
                <tr>
                    <td>电话:</td>
                    <td><input type="text" name="telephone" value="<?php echo $stu_arr['telephone'] ?>"></td>
                </tr>
                <tr>
                    <td>年龄:</td>
                    <td><input type="text" name="age" value="<?php echo $stu_arr['age'] ?>"></td>
                </tr>
                <tr>
                    <td>学院:</td>
                    <td><input type="text" name="college" value="<?php echo $stu_arr['college'] ?>"></td>
                </tr>

                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="submit" value="修改">
                        <input type="reset" name="reset" value="重置">
                    </td>
                </tr>
            </table>
        </form>
        <?php
        print_r($_SERVER["REQUEST_METHOD"]);//返回当前浏览器的请求方式
        if($_SERVER["REQUEST_METHOD"] === 'POST'){
    
    
            // print_r($_POST);

            $stu_no = $_POST['stu_no'];
            $stu_name = $_POST['stu_name'];
            $gender = $_POST['gender'];
            $telephone = $_POST['telephone'];
            $age = $_POST['age'];
            $college = $_POST['college'];
        }
        $sql = "UPDATE stu set stu_no='{
      
      $stu_no}',stu_name='{
      
      $stu_name}',
        gender='{
      
      $gender}',telephone='{
      
      $telephone}',age='{
      
      $age}',college='{
      
      $college}' where stu_no='{
      
      $stu_get}'";

        $result = $pdo -> exec($sql);
        print_r($result);

        if($result > 0){
    
    
            header('Location:index.php');
        }else{
    
    
            echo '修改失败';
        }
        ?>
    </center>
</body>
</html>

<!-- 添加页面 -->

delete.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>删除信息页面</title>
</head>
<body>
<?php 
    require 'common/conn.php';

    $stu_get = $_GET['stu_no'];
    print_R($stu_get);

    $sql = "DELETE from stu where stu_no='{
      
      $stu_get}'";

    $result = $pdo->exec($sql);

    header('Location/index.php');
    
?>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/m0_65431212/article/details/126971008