How to teach female to develop PHP from scratch (2): Form processing _ PHP Tutorial

Source: Internet
Author: User
How to teach female to develop PHP from scratch (2): process the form. After all, I underestimated the lethal effect of my sister's words on programmers, even if there is no burst photo, the clicks will arrive at four digits in less than a day. after all, I still underestimate the lethal effect of the word "sister" on programmers, today, without a picture, the number of clicks in less than a day has reached four digits, and it has become the most comments of the day! Even though all the comments were too poor, my sister said to me very affectionately-here I am very concerned about the content. When I saw this sentence, I suddenly gave birth to a kind of self-satisfaction. I don't know if my sister would be happy to lead by example?

When so many people see this series, I should stick to the purpose of the technology blog, focus on technology as much as possible, and discuss the knowledge of PHP development with the majority of developers. However, in order to take care of the majority of programmers who click on the title recently, I will add some descriptions of my sister's performance in the course of learning and teach you how to cultivate feelings with her sister, oh, no, it's academic exchange.

Since we are teaching our sister-in-law web development, especially in the case of almost zero foundation, we must start with very basic things and be patient enough. However, there must also be strict teaching ideas, so you can't say anything when you think of anything. This way, the girl will not only get bored, but also have a low learning efficiency. You think about it. my sister is tired and cannot get up with her learning skills. What else can I do with you? Before you can demonstrate your knowledge depth and rich development experience, my sister has shut down the computer and abandoned you. what else can I do ?! To sum up, there is no way, no way, no way of thinking, no patience, and you are destined to be alone for a lifetime.

I have introduced some things in the previous article, which can be summarized as follows: 1) use php code Label 2) how to use the echo statement to output html code, and escape the code with the \ escape character for "" (double quotation marks. 3) How php code is mixed with html code. Then, the next step is how to submit the form and process the submitted form.

Step 3: submit a form

In the html syntax, we use

Tags are used to identify forms. in Baidu encyclopedia, we can know that forms are mainly responsible for data collection in webpages. That is to say, the form provides the data submission function for the system. Let's repeat the lines of code we wrote earlier, focusing on the content of the form.

  
 
  1. Echo"";
  2. Echo"";
  3. Echo" ";
  4. Echo"User logon interface";
  5. ?>
  6. User name:
  7. Password:
  8. Echo""
  9. // Submit indicates the form submission button, which is displayed as "log on to the system"
  10. ?>

Some people may think that the above lines of code and comments are too simple. As I said at the beginning, the key to teaching a girl to learn technology is to be patient. if she can do anything, what should she do? Here, what I want to explain to my sister is action = "Controller. php ", that is, after clicking the submit button, the page will jump to Controller. the php file, in other words-sister, form submission, is to submit the data to Conroller. it is used to process data in php. Such an explanation may be biased, but in order to give the girl a better understanding, this is also a suitable option, and the effect is good)

Step 4: obtain and process the data in the submitted form

Since the form is submitted, the next step is how to obtain and process the data in the form. If you tell your sister the POST request and HTTP protocol at this time, she will not be able to get any effect except dizzy and give you two white eyes. You may say that your teaching method is not scientific and comprehensive. Whether science is comprehensive or not I will not argue with you for the moment. sometimes, more details do not mean that it is good for the students. it is the most important to continue the teaching process.

To put it bluntly, let's look at the Controller. php code and explain to my sister in the help of comments how PHP obtains HTML form data.

 
 
  1. Session_start (); // you must call this function before using session.
  2. $ User_id = $ _ POST ['User _ id']; // declare the variable $ user_id and assign it the value of user_id in the POST request
  3. $ User_password = $ _ POST ['User _ password'];
  4. // Declare the variable $ user_password and assign the value of user_password in the POST request to it
  5. // The. operator connects two string variables. the following two statements show the user name and password of the submitted form.
  6. Echo "the username of the submitted form is:". $ user_id;
  7. Echo "password is". $ user_password;
  8. ?>

Seeing the first sentence at the beginning: session_start (); and the following comment, the sister will certainly ask-what is session? Session, in general, is the process from the user entering the website to closing the browser. In PHP, session is used to register several session global variables and use these variables in different pages or PHP files. The sister certainly cannot understand what the session is at once. At present, she only needs to know for the time being that this is the case, and will gradually deepen her understanding in the future.

Continue to see the code. we will know from the code in step 3 that the form submission method is POST, so we use the $ _ POST variable to get the data in the form. In the brackets [''], the index is the name in the form. you can use the index to obtain the value in the array $ _ POST and display it on the page.

Displaying data on a page is a "processing" method. However, since it is a logon function, the entered user name and password must be consistent with the settings in the system. For example, if only one user name in the system is admin and the password is 123456, all user names except admin should be refused to log on, and the password should also correspond to admin, it must be 123456. The judgment code is as follows:

 
 
  1. Session_start (); // you must call this function before using session.
  2. $ User_id = $ _ POST ['User _ id']; // declare the variable $ user_id and assign it the value of user_id in the POST request
  3. $ User_password = $ _ POST ['User _ password'];
  4. // Declare the variable $ user_password and assign the value of user_password in the POST request to it
  5. // The. operator connects two string variables. the following two statements show the user name and password of the submitted form.
  6. If ($ user_id = 'admin' & $ user_password = '000000') // judgment statement, & operator, must both be true
  7. {
  8. Echo "successful login after verification ";
  9. }
  10. Else
  11. Echo "failed to verify, logon failed ";
  12. ?>

My sister worked very hard. in the morning, I asked her if she was familiar with the basic operations of the database. When I asked her what she was doing in the evening, she said: What is the database being supplemented. At this time, I did not know how much I was touched. I had to sit down in front of the computer code word when I had been tired for a day.

Why should I ask her to review the database? Because our system is unlikely to have only one account, it is impossible to write all accounts and passwords into the php file. What we need to do is store the information in the database and take it out when necessary. When should I use it? Of course, it is the time for login to verify, that is, the present.

Step 5: connect to the MySQL database

PHP provides complete functions for operating MySQL databases. these functions provide functions from connecting to the database, executing SQL statements, processing data result sets, and disabling the database. Sister can use these functions to make Web development based on MySQL databases more efficient and simple.

Then, we start from the most basic connection to the database. See the code:

 
 
  1. $ Host = 'localhost'; // defines the database server as a local host.
  2. $ User_name = 'root'; // defines the database user name
  3. $ Password = ''; // defines the password of the database.
  4. $ Conn = mysql_connect ($ host, $ user_name, $ password); // Connect to MySQL and obtain the link
  5. If (! $ Conn) {// Determine whether the link is empty
  6. Die ('database connection failed: '. mysql_error ());
  7. // The language structure die () is used. its function is similar to exit. output a piece of information and cannot immediately interrupt program execution.
  8. }
  9. Else {
  10. Echo "database connection successful ";
  11. }

Here, I assume that my sister knows what the server, user name, and password are for psychological activities: She said that she had done evil for a night, so she wouldn't even know it, and she was so scared ). There is only one key statement,

$ Conn = mysql_connect ($ host, $ user_name, $ password); $ host (server), $ user_name username), and $ password) as parameters, use the mysql_connect function to obtain the link of the mysql database and assign it to the variable $ conn.
Then, how can I use the database link $ conn to operate the mysql database? We will continue in the next article tomorrow.

...

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.