jarod8016b 发表于 2017-3-31 10:40:13

php 向数据库表中插入数据

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>
页: [1]
查看完整版本: php 向数据库表中插入数据