PHP implements inserting data into database table

小云云
Release: 2023-03-22 12:12:01
Original
14704 people have browsed it

This article mainly shares with you how to insert data into the database table in PHP. It is mainly shared with you in the form of code. I hope it can help you.

php insert data page 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>
Copy after login

Process data insertion page processInsertData.php

<?php
    $link=mysql_connect("localhost","root","root");
    mysql_select_db("rorely");
    $id=$_POST[&#39;txtId&#39;]|null;
    $name=$_POST[&#39;txtName&#39;];
    $age=$_POST[&#39;txtAge&#39;];
    $sex=$_POST[&#39;sSex&#39;];
    $address=$_POST[&#39;txtAddress&#39;];
    $exec="insert into test values($id,&#39;$name&#39;,$age,&#39;$sex&#39;,&#39;$address&#39;)";
    $result=mysql_query($exec);
    if($result) echo "学生已添加到数据表中<br>";
    else echo "该学生没有添加进数据表,请重新输入。<br>";
    mysql_close();
?>
<a href="insertData.php">返回添加页面</a>
Copy after login

Related recommendations:

Detailed explanation of the simple extension class of Yii framework batch insert data

Introduction to how to optimize MYSQL batch insert data

php multi-process insert data

The above is the detailed content of PHP implements inserting data into database table. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!