PHP案例:學生信息管理系統

-- Database: testjavascript

-- 表的結構 messagephp

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>
<!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;
}
?>
相關文章
相關標籤/搜索