Parse PHP to send data to MySQL

Source: Internet
Author: User
Tags html form php script mysql database

The following piece of code is a very simple HTML form:

<body>
<form action=submitform.php3 method=GET>
姓 : <input type=text name=first_name size=25 maxlength=25>
名 : <input type=text name=last_name size=25 maxlength=25>
<p>
<input type=submit>
</form>
</body>

When you enter the data and press the Submit button, the form will send the data to the SUBMITFORM.PHP3. This PHP script is then processed to receive the data, the following is the SUBMITFORM.PHP3 code:

<body>
<?php
mysql_connect (localhost, username, password);
mysql_select_db (dbname);
mysql_query ("INSERT INTO tablename (first_name, last_name)
VALUES ('$first_name', '$last_name')
");
print ($first_name);
print (" ");
print ($last_name);
print ("<p>");
print (" 感谢填写注册表 ");
?>
</body>

The "username" and "password" in the third line of the code represent the account and password you logged into the MySQL database. The "dbname" in line fifth indicates the name of the MySQL database. The "tablename" in line 13th is the name of a data table in the database.

When you press submit, you can see that the name you entered is displayed on a new page. Take a look at the URL bar of the browser and it should be something like this:

.../submitform.php3?first_name=fred&last_name=flintstone

Because we use the form get method, the data is routed to submitform.php3 through the URL. Obviously, the Get method is limited, and when there is a lot of content to be passed, you can't use GET, only the POST method. But no matter what the method, when the data transfer is complete, PHP automatically creates a variable that is the same as their name (the form's Name property) for each field in the form.

PHP variables have all started with a dollar sign, so that in the process of SUBMITFORM.PHP3 script processing, there will be $first _name and $last _name These two variables, the contents of the variable is what you entered.

Let's check to see if the name you typed is actually entered into the database. To start MySQL, at the mysql> prompt, enter:

Mysql> SELECT * FROM TableName;

You should be able to get a table with the content that you just typed:

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

| first_name | last_name |

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

| Willow | such as wind

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

1 rows in Set (0.00 sec)

Let's analyze how SUBMITFORM.PHP3 works:

The first two lines of the script are:

mysql_connect (localhost, username, password);
mysql_select_db (dbname);

These two function calls are used to open the MySQL database, the meaning of the specific parameters has just been said.

The following line executes an SQL statement:

mysql_query ("INSERT INTO tablename (first_name, last_name)
VALUES ('$first_name', '$last_name')
");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.