PHP/MySQL 3-Day 2 (2)

Source: Internet
Author: User
4. it is no longer difficult to read data from the database to send data to the server. But how can we send data to the database in turn? In fact, this is not a problem with PHP. First, create a webpage with a simple table. $ #@ 60; htmSyntaxHighlighter. all (); 4. send data to the server

It is no longer difficult to read data from the database. But how can we send data to the database in turn? In fact, this is not a problem with PHP.

First, create a webpage with a simple table.

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62;

Name: $ # @ 60; input type = "Text" name = "first" $ # @ 62; $ # @ 60; br $ # @ 62;

Last name: $ # @ 60; input type = "Text" name = "last" $ # @ 62; $ # @ 60; br $ # @ 62;

Address: $ # @ 60; input type = "Text" name = "address" $ # @ 62; $ # @ 60; br $ # @ 62;

Position: $ # @ 60; input type = "Text" name = "position" $ # @ 62; $ # @ 60; br $ # @ 62;

$ # @ 60; input type = "Submit" name = "submit" value = "input information" $ # @ 62;

$ # @ 60;/form $ # @ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

Pay attention to the usage of $ PATH_INFO. As I mentioned in the first lesson, you can use PHP anywhere in the HTML code. You will also notice that each element in the table corresponds to a field in the database. This correspondence is not necessary. it is more intuitive to understand the code later.

Note that the name attribute is added to the Submit button. In this way, I can test whether the $ submit variable exists in the program. When the webpage is called again, I will know whether the form has been filled in when the page is called.

I should point out that you do not have to write the content of the above webpage to the PHP program, and then return to call the program itself. You can separate the web page for displaying a table from the program for processing a table on two web pages, three web pages, or even more web pages. Put it in a file to make the content more compact.

Now let's add some code to check the content entered by the user in the table. I will display all the query parameter variables with $ HTTP_POST_VARS, just to prove that PHP has actually passed all the variables to the program. This method is a useful debugging method. To view all the variables, use $ GLOBALS.

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

If ($ submit ){


// Process table input

While (list ($ name, $ value) = each ($ HTTP_POST_VARS )){

Echo "$ name = $ value $ # @ 60; br $ # @ 62 ;";

}

} Else {


// Display the table

? $ #@ 62;

$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62;

Name: $ # @ 60; input type = "Text" name = "first" $ # @ 62; $ # @ 60; br $ # @ 62;

Last name: $ # @ 60; input type = "Text" name = "last" $ # @ 62; $ # @ 60; br $ # @ 62;

Address: $ # @ 60; input type = "Text" name = "address" $ # @ 62; $ # @ 60; br $ # @ 62;

Position: $ # @ 60; input type = "Text" name = "position" $ # @ 62; $ # @ 60; br $ # @ 62;

$ # @ 60; input type = "Submit" name = "submit" value = "input information" $ # @ 62;

$ # @ 60;/form $ # @ 62;

$ #@ 60 ;? Php

} // End if, if ends

? $ #@ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

There are two pages in this news. Currently, there are two pages in page 1st.


The program runs normally now, so we can get the input content in the table and send them to the database.
  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

If ($ submit ){


// Process table input

$ Db = mysql_connect ("localhost", "root ");

Mysql_select_db ("mydb", $ db );

$ SQL = "INSERT INTO employees (first, last, address, position)
VALUES ($ first, $ last, $ address, $ position )";

$ Result = mysql_query ($ SQL );

Echo "Thank you! Information entered .";

} Else {


// Display the table content

? $ #@ 62;

$ #@ 60; form method = "post" action = "$ # @ 60 ;? Php echo $ PATH_INFO? $ # @ 62; "$ # @ 62;

Name: $ # @ 60; input type = "Text" name = "first" $ # @ 62; $ # @ 60; br $ # @ 62;

Last name: $ # @ 60; input type = "Text" name = "last" $ # @ 62; $ # @ 60; br $ # @ 62;

Address: $ # @ 60; input type = "Text" name = "address" $ # @ 62; $ # @ 60; br $ # @ 62;

Position: $ # @ 60; input type = "Text" name = "position" $ # @ 62; $ # @ 60; br $ # @ 62;

$ # @ 60; input type = "Submit" name = "submit" value = "input information" $ # @ 62;

$ # @ 60;/form $ # @ 62;

$ #@ 60 ;? Php

} // End if, if ends


? $ #@ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

You have already inserted data into the database. However, there is still a lot of work to do. What if the user does not enter a column? What should I do if I fill in the text where a number is needed? Or fill in the wrong information. what should I do? Don't worry. Let's take a step.

There are two pages in this news. Currently, there are two pages in page 2nd.


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.