I have introduced some things in the previous article, which can be summarized as follows: 1) how to use tags to mark php code (2) how to use echo statements to output html code, in addition, I still underestimate the lethal effect of the word "sister" on programmers after all. today, even if there were no burst photos, the clicks reached four digits in less than a day and became the most comments on 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 Tag identification (2) how to use the echo statement to output html code, and escape 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.
-
- Echo"";
- Echo"";
- Echo" ";
- Echo"User logon interface";
- ?>
-
-
-
-
-
- User name:
-
- Password:
-
-
- Echo""
- // Submit indicates the form submission button, which is displayed as "log on to the system"
- ?>
-
-
-
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.
-
- Session_start (); // you must call this function before using session.
-
- $ User_id = $ _ POST ['User _ id']; // declare the variable $ user_id and assign it the value of user_id in the POST request
- $ User_password = $ _ POST ['User _ password'];
- // Declare the variable $ user_password and assign the value of user_password in the POST request to it
-
- // The. operator connects two string variables. the following two statements show the user name and password of the submitted form.
- Echo "the username of the submitted form is:". $ user_id;
- Echo "password is". $ user_password;
- ?>
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:
-
- Session_start (); // you must call this function before using session.
-
- $ User_id = $ _ POST ['User _ id']; // declare the variable $ user_id and assign it the value of user_id in the POST request
- $ User_password = $ _ POST ['User _ password'];
- // Declare the variable $ user_password and assign the value of user_password in the POST request to it
-
- // The. operator connects two string variables. the following two statements show the user name and password of the submitted form.
- If ($ user_id = 'admin' & $ user_password = '000000') // judgment statement, & operator, must both be true
- {
- Echo "successful login after verification ";
- }
- Else
- Echo "failed to verify, logon failed ";
- ?>
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:
-
- $ Host = 'localhost'; // defines the database server as a local host.
- $ User_name = 'root'; // defines the database user name
- $ Password = ''; // defines the password of the database.
-
- $ Conn = mysql_connect ($ host, $ user_name, $ password); // Connect to MySQL and obtain the link
- If (! $ Conn) {// Determine whether the link is empty
- Die ('database connection failed: '. mysql_error ());
- // The language structure die () is used. its function is similar to exit. output a piece of information and cannot immediately interrupt program execution.
- }
- Else {
- Echo "database connection successful ";
- }
Here, I assume that my sister knows what the server, user name, and password are. (psychological activity: The girl said that it had been done for one 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 (user name), and $ password (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.
Link: http://www.cnblogs.com/levenyes/p/3302303.html