search
HomeBackend DevelopmentPHP TutorialHow 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

Jul 13, 2016 pm 05:19 PM
phpclassmatedeal withhowgirlrightdevelopstartIofprogramformstillconductzero

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 class="tag-name">html</span><span class="tag">></span><span>";  </span></span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span>";  </span></span>
</li>
<li>
<span>echo "</span><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">charset</span><span>=\"gbk\"</span><span class="tag">></span><span>";  </span></span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><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></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 class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><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></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 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 class="tag-name">td</span><span class="tag">></span><span class="tag"><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></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 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 class="tag-name">td</span><span class="tag">></span><span class="tag"><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></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 class="tag-name">td</span><span class="tag">></span><span class="tag"><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></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>
</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></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></li>
</ol>

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

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

第五步:连接MySQL数据库

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

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

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></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
PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Debunking the Myths: Is PHP Really a Dead Language?Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AM

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

The PHP vs. Python Debate: Which is Better?The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor