編碼的世界 / 優質文選 / 文明

php 向數據庫表中插入數據


2021年7月04日
-   

php插入數據頁面insertData.php
<html>
    <head>
        <title>向數據庫中插入數據</title>
    </head>
    <body>
        <center>
            <form name="myForm" method="post" action="processInsertData.php">
                <table bgcolor="cyan">
                    <tr>
                        <td>學號</td>
                        <td><input type="text" name="txtId"  /></td>
                    </tr>
                    <tr>
                        <td>姓名</td>
                        <td><input type="text" name="txtName" /></td>
                    </tr>
                    <tr>
                        <td>年齡</td>
                        <td><input type="text" name="txtAge"></td>
                    </tr>
                    <tr>
                        <td>性別</td>
                        <td>
                            <select name="sSex">
                                <option value="male">male</option>
                                <option value="female">female</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>地址</td>
                        <td><input type="text" name="txtAddress"/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" name="submit" value="添加"/>
                            <input type="reset" name="reset" value="重置">
                        </td>
                    </tr>
                </table>
            </form>
        </center>
    </body>
</html>
處理數據插入頁面processInsertData.php
<?php
    $link=mysql_connect("localhost","root","root");
    mysql_select_db("rorely");
    $id=$_POST['txtId']|null;
    $name=$_POST['txtName'];
    $age=$_POST['txtAge'];
    $sex=$_POST['sSex'];
    $address=$_POST['txtAddress'];
    $exec="insert into test values($id,'$name',$age,'$sex','$address')";
    $result=mysql_query($exec);
    if($result) echo "學生已添加到數據表中<br>";
    else echo "該學生沒有添加進數據表,請重新輸入。<br>";
    mysql_close();
?>
<a href="insertData.php">返回添加頁面</a>

熱門文章