1. 程式人生 > >PHP 面向過程 - 儲存、註冊、修改、刪除

PHP 面向過程 - 儲存、註冊、修改、刪除

homepage.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>資訊</title>
</head>
<body>
    <h1 style="text-align: center">使用者資訊首頁</h1>
    <table border="3px" width="1000" bgcolor="#f0fff0" align="center">
        <tr>
            <th>賬號</th>
            <th>密碼</th>
            <th>姓名</th>
            <th>性別</th>
            <th>年齡</th>
            <th>手機號</th>
            <th>操作</th>
        </tr>
    <?php
        include "config.php";
        $link = mysqli_connect(HOST,USER,PASSWORD,DATABASE);
        mysqli_set_charset($link,"utf8");
        $sql_1 = "select * from"."  text";
        $result = mysqli_query($link,$sql_1);
        $get_result = mysqli_fetch_all($result,MYSQLI_ASSOC);
        foreach ($get_result as $value){
            echo "<tr>";
            echo "<td style='text-align: center'>".$value["number"]."</td>";
            echo "<td style='text-align: center'>".$value["password"]."</td>";
            echo "<td style='text-align: center'>".$value["name"]."</td>";
            echo "<td style='text-align: center'>".$value["sex"]."</td>";
            echo "<td style='text-align: center'>".$value["age"]."</td>";
            echo "<td style='text-align: center'>".$value["phone"]."</td>";
            echo "<td style='text-align: center'><a href='receive.php?type=delete&id=".$value["id"]."'>刪除</a>&emsp;<a href='modify.php?id=".$value["id"]."'>修改</a> </td>";
            echo "</tr>";
        }
    ?>
    </table>
    <div align="center">
        <a href="landing.php">返回</a>&emsp;
    </div>
</body>
</html>

register.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>註冊</title>
</head>
<body>
    <h1 style="text-align: center">新使用者註冊</h1>
    <form method="post" action="receive.php?type=register">
        <table align="center">
        <tr>
            <td>
                賬&emsp;&emsp;號:<input type="text" name="num"/>
            </td>
        </tr>
        <tr>
            <td>
                密&emsp;&emsp;碼:<input type="password" name="pass"/>
            </td>
        <tr>
            <td>
                確認密碼:<input type="password" name="re_pass"/>
            </td>
        </tr>
        <tr>
            <td>
                姓&emsp;&emsp;名:<input type="text" name="name">
            </td>
        </tr>
        <tr>
            <td>
                性&emsp;&emsp;別:
                &emsp;男<input type="radio" name="sex" value="men">
                &emsp;女<input type="radio" name="sex" value="women">
            </td>
        </tr>
        <tr>
            <td>
                年&emsp;&emsp;齡:<input type="text" name="age">
            </td>
        </tr>
        <tr>
            <td>
                手&nbsp;&nbsp;機&nbsp;&nbsp;號:<input type="text" name="phone">
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <input type="submit" value="確認註冊">
            </td>
        </tr>
        </table>
    </form>
</body>
</html>
    

modify.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改資訊</title>
</head>
<body>
<h2 align="center">修改資訊</h2>
<?php
    include "config.php";
    $link = mysqli_connect(HOST,USER,PASSWORD,DATABASE);
    mysqli_set_charset($link,"utf8");
    $id = $_GET["id"];
    $sql = "select * "."from text where id={$id}";
    $result = mysqli_query($link,$sql);
    $data = mysqli_fetch_assoc($result);    //mysql_fetch_assoc() 從結果集中取得一行作為關聯陣列
?>
<form action="receive.php?id=<?php echo $id ?>&type=modify" method="post">
    <table align="center">
        <tr>
            <td>賬&emsp;&emsp;號:<input type="text" name="num" value="<?php echo $data["number"] ?>"/></td>
        </tr>
        <tr>
            <td>密&emsp;&emsp;碼:<input type="password" name="pass"/></td>
        <tr>
        <tr>
            <td>確認密碼:<input type="password" name="re_pass"/></td>
        </tr>
        <tr>
            <td>姓&emsp;&emsp;名:<input type="text" name="name" value="<?php echo $data["name"] ?>"></td>
        </tr>
        <tr>
            <td>
                性&emsp;&emsp;別:
                &emsp;男<input type="radio" name="sex" value="男"<?php echo $data['sex']=='男'?'checked':'' ?>>
                &emsp;女<input type="radio" name="sex" value="女"<?php echo $data['sex']=='女'?'checked':'' ?>>
            </td>
        </tr>
        <tr>
            <td>年&emsp;&emsp;齡:<input type="text" name="age" value="<?php echo $data["age"] ?>"></td>
        </tr>
        <tr>
            <td>手&nbsp;&nbsp;機&nbsp;&nbsp;號:<input type="text" name="phone" value="<?php echo $data["phone"] ?>"></td>
        </tr>
        <tr align="center">
            <td><br/><input type="submit" value="確認修改">&emsp;<a href="landing.php">返回登陸</a></td>
        </tr>
    </table>
</form>
</body>
</html>

recevie.php

<?php
    include_once "config.php";
    $link = mysqli_connect(HOST,USER,PASSWORD,DATABASE);
    mysqli_set_charset($link,"utf8");
    isset($_GET['id'])?$id=$_GET['id'] : $id='';
    $type = $_GET["type"];
    switch ($type){
        case "modify":  //修改
            $sql_3 = "update text set"." number='".$_POST['num']."',"."password='".$_POST["pass"]."',"."name='".
                $_POST["name"]."',"."sex='".$_POST["sex"]."',"."age='".$_POST["age"]."',"."phone='".$_POST["phone"].
                "' where"." id='".$id."';";
            //echo $sql_3;
            if (!$_POST["pass"]){
                echo "請正確輸入密碼&emsp;";
            }
            if ($_POST["pass"] !== $_POST["re_pass"]){
                echo "請確認兩次密碼是否一致&emsp;";
            }
            mysqli_query($link,$sql_3);
            if (mysqli_affected_rows($link)>0){
                header("location:homepage.php");
            }else{
                echo "修改失敗";
            }
            break;
        case "delete":  //刪除
            $sql_4 = "delete from"." text where id='".$id."';";
            mysqli_query($link,$sql_4);
            header("location:homepage.php");
            break;
        case "register":    //註冊
            $number = $_POST["num"];
            $password = $_POST["pass"];
            $re_password = $_POST["re_pass"];
            $name = $_POST["name"];
            $sex = $_POST["sex"];
            $age = $_POST["age"];
            $phone = $_POST["phone"];
            if ($password !== $re_password){    //判斷密碼是否一致
                die("請確認兩密碼是否一致");
            }
            if (!$link){    //判斷使用者名稱是否已使用
                die("資料庫連線失敗");
            }
            mysqli_set_charset($link,"utf8");
            $sql_1 = "select number "."from text";
            $data_1 = mysqli_query($link,$sql_1);
            $result_1 = mysqli_fetch_all($data_1);
            foreach ($result_1 as $value){
                if (in_array($number,$value)){
                    die("此使用者名稱已被註冊");
                }
            }
            switch ($sex){  //判斷性別
                case "men";
                    $_sex = "男";
                    break;
                case "women";
                    $_sex = "女";
                    break;
            }
            //新增使用者
            $sql_2 = "insert into"." text value(NULL,{$number},{$password},'{$name}','{$_sex}',{$age},{$phone})";
            $data_2 = mysqli_query($link,$sql_2);
            if ($data_2){
                header("location:homepage.php");
            }else{
                echo "註冊失敗";
            }
    }
// echo mysqli_insert_id($link);   //可以用來獲取最後插入那條資料的id號



config.php

<?php
    define("HOST","localhost");
    define("USER","root");
    define("PASSWORD","985211");
    define("DATABASE","text");