search
Homephp教程php手册详细解析 PHP 向 MySQL 发送数据过程

详细解析 PHP 向 MySQL 发送数据过程

Jun 06, 2016 pm 07:56 PM
htmlmysqlphpsenddataparsedetailedprocess

html body formaction=submitform.php3method=GET 姓:inputtype=textname=first_namesize=25maxlength=25 名:inputtype=textname=last_namesize=25maxlength=25 p inputtype=submit /form /body /html 当你输入数据,并按下 submit 按钮后,这个表单将把数据

 


姓:
名:





    当你输入数据,并按下 submit 按钮后,这个表单将把数据发送到 submitform.php3 。再由这个 PHP 脚本来处理收到的数据,下面就是 submitform.php3 的代码:

 

mysql_connect(localhost,username,password);
mysql_select_db(dbname);
mysql_query("INSERTINTOtablename(first_name,last_name)
VALUES('$first_name','$last_name')
");
print($first_name);
print("");
print($last_name);
print("

");
print("感谢填写注册表");
?>

    在代码的第三行中的 "username" 和 "password" 分别代表你登陆 MySQL 数据库的账号和密码。在第五行中的 "dbname" 表示 MySQL 数据库的名称。在第十三行中的 "tablename" 是数据库中的一个数据表的名称。

    当你按下 submit 之后,可以看到你输入的名字被显示在一个新的页面中。再看一看浏览器的 URL 栏,它的内容应该是像这样的:

 … /submitform.php3?first_name=Fred&last_name=Flintstone

    因为我们用到的是表单 GET 方法,因此数据是通过 URL 来传送到 submitform.php3 的。显然, GET 方法是有局限性的,当要传递的内容很多时,就不能用 GET 了,只能用 POST 方法。但不管用什么方法,当数据传送完成后, PHP 自动的为每一个表单中的字段建立一个和他们的名字(表单的 name 属性)相同的变量。

    PHP 变量都已用一个美元符号开头的,这样,在 submitform.php3 脚本处理的过程中,就会有 $first_name 和 $last_name 这两个变量了,变量的内容就是你输入的内容。

    我们来检查一下你输入的名字是否真的被输入到数据库中了。启动 MySQL, 在 mysql> 提示符下输入:

 mysql> select * from tablename;

    你应该可以得到一个表,内容就是你刚才输入的了:

  ------------ ------------

| first_name | last_name |

------------ ------------

| 柳 | 如风

------------ ------------

1 rows in set (0.00 sec)

   

[1] [2] [3] 

详细解析 PHP 向 MySQL 发送数据过程

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment