Home >Backend Development >PHP Tutorial >How to teach female students to develop PHP from scratch (2): Form processing_PHP tutorial

How to teach female students to develop PHP from scratch (2): Form processing_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:19:04863browse

After all, I still underestimated the lethality of the word "girl" to programmers. In today's world where there is no picture to say something, I can still get clicks in less than a day without making a splash. It reached four digits and also became the most commented post of the day! Although the comments were all crooked, the girl still said something to me very thoughtfully - she expressed her concern about the content. When I saw this sentence, I immediately felt a feeling of knowing that I would give up my life and die. I wonder if I would be happy to pledge my life to my sister?

With so many people seeing this series, I should still adhere to the purpose of a technical blog, try to focus on technology, and discuss PHP development knowledge with beginners. However, in order to take care of the majority of programmers who have clicked on the title recently, I will add some descriptions of the girls' performance in the learning process to the blog, and teach you how to cultivate relationships with girls, oh, no, it is academic exchanges.

Since you are teaching web development to girls, especially when you have almost zero foundation, you must start with very basic things and have enough patience. But you must also have strict teaching ideas, and you cannot just say whatever comes to mind. Not only will the girls feel annoyed, but their learning efficiency will also be low. Think about it, if the girl is annoyed and doesn’t lose her motivation to study, what else will happen to you in the future? Before you have time to show your depth of knowledge and rich development experience, the girl has already turned off the computer and abandoned you. Then, what else can happen? ! To sum up: If you have no methods, no ideas, no patience, you are doomed to live a lonely life.

In the previous article, we have basically introduced some things, which can be summarized as follows: 1) PHP code should be identified with the tag 2) How to use the echo statement to output html code, and Regarding the use of escape characters for "" (double quotation marks). 3) How is the php code mixed with the html code. Then, on this basis, the next step is how to submit the form and all Handling of form submissions

Step 3: Submit the form

In HTML syntax, we will use the

tag to identify the form. In Baidu Encyclopedia, we can know that the form is mainly responsible for the data collection function in the web page. In other words, the form provides the system with the function of submitting data. So, let’s review the lines of code we wrote before, focusing on the content of the form part.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span>         //php代码部分开始  </span></span></li><li><span>echo "</span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>";  </span></span></li>
<li class="alt">
<span>echo "</span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>";  </span>
</li>
<li>
<span>echo "</span><span class="tag"><</span><span class="tag-name">meta</span><span> </span><span class="attribute">charset</span><span>=\"gbk\"</span><span class="tag">></span><span>";  </span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>用户登录界面</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span>";  </span>
</li>
<li>
<span class="tag">?></span><span class="comments"><!--  php代码部分结束--></span><span> </span>
</li>
<li class="alt">
<span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"login"</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"Controller.php"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="tag">></span><span> </span>
</li>
<li>
<span> </span><span class="comments"><!-- 上一行代码的意思是 --></span><span> </span>
</li>
<li class="alt">
<span> </span><span class="comments"><!-- name="login"表单的名称为login,提交的动作为&ldquo;Controller.php&rdquo;,请求方式为&ldquo;post&rdquo; --></span><span> </span>
</li>
<li>
<span>     </span><span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"tbl"</span><span class="tag">></span><span>用户名:</span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"user_id"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span> </span><span class="comments"><!-- name="user_id"的意思是所填写的用户名标识为user_id --></span><span>     </span>
</li>
<li>
<span>     </span><span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"tbl"</span><span class="tag">></span><span>密码:</span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"user_password"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span> </span><span class="comments"><!-- name="user_password"的意思是所填写的密码标识为user_password --></span><span> </span>
</li>
<li>
<span class="tag"><?</span><span class="tag-name">php</span><span>      </span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=\"submit\" </span><span class="attribute">value</span><span>=\"登入系统\"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span>"  </span>
</li>
<li><span>  //submit表示的是表单的提交按钮,按钮显示为“登入系统”  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li>
<span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span> </span>
</li>
<li class="alt">
<span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span>
</li>
<li>
<span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span>
</li>
</ol>

Seeing the above lines of code and comments, some people may think: This is too simple. I said at the beginning that the key to teaching girls to learn skills is to be patient. If a girl knows everything, why do you need her? Here, the important thing to explain to the girl is action="Controller.php", which means that after clicking the submit button, the page will jump to the file Controller.php. In other words - girl, submit the form, Just submit the data to Controller.php and let it process the data. This explanation may be biased, but in order to let the girl understand better, it is also an expedient measure and the effect is good)

Step 4: Obtain and process the data of the submitted form

Now that the form has been submitted, the next step must be how to obtain and process the data in the form. If you tell a girl about POST requests and HTTP protocols at this time, she will get confused and give you two blank looks, but you will probably get no effect. You may say that your teaching method is very unscientific and incomplete. I won’t argue with you whether it’s scientific or comprehensive. Sometimes, explaining things in detail doesn’t mean it’s good for students. Being able to carry out the teaching process is the most important thing.

Without further ado, let’s look directly at the code of Controller.php and explain to the girl how PHP obtains HTML form data with the help of comments.

<ol class="dp-xml"><li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>session_start();//使用session前必须调用该函数  </span></li><li class="alt"><span> </span></li><li><span>$</span><span class="attribute">user_id</span><span> =$_POST['user_id'];//声明变量$user_id,把POST请求中的user_id的值赋给它  </span></li><li class="alt"><span>$</span><span class="attribute">user_password</span><span>= $_POST['user_password'];  </span></li><li><span>//声明变量$user_password,把POST请求中的user_password的值赋给它  </span></li><li class="alt"><span> </span></li><li><span>//.操作符连接两个字符串变量,以下两条语句显示所提交表单的用户名和密码  </span></li><li class="alt"><span>echo "所提交表单的用户名是:".$user_id;  </span></li><li><span>echo "   密码是".$user_password;  </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>

Seeing the first sentence at the beginning: session_start(); and the following comments, girls will definitely ask - what is session? Session, in layman's terms, is the process from when the user enters the website to when the user closes the browser. Using session in PHP is to register several session global variables and use these variables in different pages or PHP files. There is definitely no way for a girl to understand what a session is all at once. For now, just let her know that there is such a thing for the time being, and her understanding of it will gradually deepen in the future study.

Continue to see the code. Comparing the code in the third step, we will know that the way to submit the form is POST, so we use the $_POST variable to obtain the data in the form. The index within the square brackets [' '] is the name in the form. The value in the array $_POST is obtained through the index, and finally displayed on the page.

Displaying data on the page is certainly a way of "processing". But since it is a login function, the username and password entered must generally match the settings in the system. For example, there is only one user name in the system which is admin and the password is 123456. Then any user name other than admin that is entered should be denied login, and the password should also correspond to admin and must be 123456. The judgment code is as follows:

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li><span>session_start();</span><span class="comment">//使用session前必须调用该函数 </span><span> </span></li><li class="alt"><span> </span></li><li><span class="vars">$user_id</span><span> =</span><span class="vars">$_POST</span><span>[</span><span class="string">'user_id'</span><span>];</span><span class="comment">//声明变量$user_id,把POST请求中的user_id的值赋给它 </span><span> </span></li><li class="alt"><span class="vars">$user_password</span><span>= </span><span class="vars">$_POST</span><span>[</span><span class="string">'user_password'</span><span>];  </span></li><li><span class="comment">//声明变量$user_password,把POST请求中的user_password的值赋给它 </span><span> </span></li><li class="alt"><span> </span></li><li><span class="comment">//.操作符连接两个字符串变量,以下两条语句显示所提交表单的用户名和密码 </span><span> </span></li><li class="alt"><span class="keyword">if</span><span>(</span><span class="vars">$user_id</span><span>==</span><span class="string">'admin'</span><span>&&</span><span class="vars">$user_password</span><span>==</span><span class="string">'123456'</span><span>)</span><span class="comment">//判断语句,&&为且运算符,必须两条件均符合才为真 </span><span> </span></li><li><span>{  </span></li><li class="alt"><span>    </span><span class="func">echo</span><span> </span><span class="string">"通过验证,登录成功"</span><span>;  </span></li><li><span>}  </span></li><li class="alt"><span class="keyword">else</span><span>   </span></li><li><span>    </span><span class="func">echo</span><span> </span><span class="string">"未通过验证,登录失败"</span><span>;  </span></li><li class="alt"><span>?> </span></span></li></ol>

妹子很努力,上午我问她对数据库的基本操作是不是熟悉,晚上我问她在干什么的时候,她说:在恶补数据库呢。这时候,我的心里不知道多感动,本来上了一天班挺累的顿时疲劳尽消赶紧坐到电脑前码字。

为什么我要让她复习数据库呢?因为我们的系统不大可能只有一个账号,更不可能把所有的账号和密码都写进PHP文件中。我们要做的是,把这些信息存进数据库里,到需要用的时候就拿出来。什么时候需要用呢?当然是登录需要验证的时候,也就是现在。

第五步:连接MySQL数据库

PHP提供了完成的操作MySQL数据库的函数,这些函数提供了从连接数据库、执行SQL语句、处理数据结果集到关闭数据库等方方面面的功能。妹子可以用通过这些函数使得基于MySQL数据库的Web开发变得高效而且简单。

那么,我们就从最基本的连接数据库开始。请看代码:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li>
<li>
<span>  </span><span class="vars">$host</span><span> =</span><span class="string">'localhost'</span><span>;</span><span class="comment">//定义数据库服务器,为本地主机 </span><span> </span>
</li>
<li class="alt">
<span>  </span><span class="vars">$user_name</span><span>=</span><span class="string">'root'</span><span>; </span><span class="comment">//定义数据库的用户名 </span><span> </span>
</li>
<li>
<span>  </span><span class="vars">$password</span><span> =</span><span class="string">''</span><span>;     </span><span class="comment">//定义数据库的密码 </span><span> </span>
</li>
<li class="alt"><span> </span></li>
<li>
<span>  </span><span class="vars">$conn</span><span> = mysql_connect(</span><span class="vars">$host</span><span>,</span><span class="vars">$user_name</span><span>,</span><span class="vars">$password</span><span>);</span><span class="comment">//连接MySQL,获取链接 </span><span> </span>
</li>
<li class="alt">
<span>  </span><span class="keyword">if</span><span>(!</span><span class="vars">$conn</span><span>){</span><span class="comment">//判断链接是否为空 </span><span> </span>
</li>
<li>
<span>    </span><span class="keyword">die</span><span>(</span><span class="string">'数据库连接失败:'</span><span>.mysql_error());  </span>
</li>
<li class="alt">
<span>    </span><span class="comment">//使用了语言结构die(),它的功能类似于exit,输出一段信息不能并立即中断程序的执行 </span><span> </span>
</li>
<li><span>  }  </span></li>
<li class="alt">
<span>  </span><span class="keyword">else</span><span>{  </span>
</li>
<li>
<span>      </span><span class="func">echo</span><span> </span><span class="string">"数据库连接成功"</span><span>;  </span>
</li>
<li class="alt"><span>  } </span></li>
</ol>

在这里,我假定妹子知道什么是服务器、用户名和密码心理活动:妹子说恶补了一个晚上,该不会连这个都不知道吧,心惊胆战ing)。关键的语句其实就只有一句,

$conn = mysql_connect($host,$user_name,$password);$host(服务器)、$user_name用户名)和$password密码)作为参数,通过mysql_connect函数获取mysql数据库的链接,并赋给变量$conn。
那么,如何利用数据库链接$conn对mysql数据库进行操作呢?我们明天在下一篇再继续说。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/601049.htmlTechArticle我终究还是低估了妹子两个字对程序猿们的杀伤力,在无图说个杰宝的今天,竟然还能在没有爆照的情况下点击量一天不到就到达了四位数...
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