php+script簡単な学生管理システムを実現

54504 ワード

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 "alert(''); window.location='index.php';";
        }else{
            echo "alert(''); window.history.back();";
        }
    break;

    case "del"://    
        $id = $_GET['id'];
        $sql2 = "delete from student where id = {$id}";
        $pdo->exec($sql2);
        header('location:index.php');
        // echo  
       // echo "location='index.php';";
    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 "alert(''); window.location='index.php';";
        }else{
            echo "alert(''); window.history.back();";
        }
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>