Php/mysql 3rd-The third day (i)

Source: Internet
Author: User
Tags functions header html form mysql php file php code strlen mysql database
First, the basic function

Welcome to the third and final lesson of this tutorial. If you've learned the first and second lessons, you've mastered the basics of installing and programming MySQL and PHP. Here are some of the other functions of PHP that might be useful to you and make your development process simpler. First, let's look at the document.

You should know some basic concepts of the header file, right? The header file is an external file whose contents are included in the main program. The method is also very simple: referencing the header filename in the program file, the header file is included. Using a header file in PHP involves two functions: include () and require (). These two functions are very small, but very important, so we have to study carefully. The Require () function works like a xssi, and regardless of which part of the program is used, the contents of the header file are processed as part of the program itself, as soon as the program starts running. Therefore, if you use the Require () function in a conditional decision statement, the header file is included even if the condition is not true.

The Include () function only contains the contents of the head file when executing to this statement. If the program is not running here, PHP will not be able to control it. This means that when you use include in the Conditional decision section, it works exactly the way you want it to.

Also, if you use the Require () function and the header file you specified does not exist, the program will stop running and produce an error. If you use include (), the program will produce a warning message, but it will continue to run. You can try it yourself, run the following program, and then replace the include () with require () and compare the results of the two programs.

$#@60;html$#@62;

$#@60;body$#@62;



$#@60;? Php

Include ("Emptyfile.inc");

echo "Hello World";

? $#@62;



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

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

I like the suffix name of the head file as. Inc, so that you can distinguish the head file from the general program. If you do the same, please modify the Web server Software configuration file so that it can handle the. inc file as a PHP file. Otherwise, hackers may guess your header file name, and then use the browser to display the contents of the head file in plain text format. At this point, if you have some confidential information (such as a database password, etc.) in your header file, that would be bad. <

So, what do you do with a header file? Very simple! Put the content that is common to all programs in the header file. Like HTML file headers, footnotes, database connection codes, and some of the functions you've defined yourself. Copy the following text into a file and save it as Header.inc.

  $#@60;? Php

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

mysql_select_db ("MyDB", $db);

? $#@62;

$#@60;html$#@62;

$#@60;head$#@62;

$#@60;title$#@62;

$#@60;? php echo $title? $#@62;

$#@60;/title$#@62;

$#@60;/head$#@62;

$#@60;body$#@62;

$#@60;center$#@62;$#@60;h2$#@62;$#@60;? php echo $title? $#@62;$#@60;/h2$#@62;$#@60;/center$#@62;

Then create another file, the name is Footer.txt, which can contain some of the text and tags used at the end of the program.

Now we're going to create a file with the actual PHP code inside the file. Try the following code, of course, to confirm that the MySQL database server is running.

  $#@60;? Php

$title = "Hello World";

Include ("Header.inc");

$result = mysql_query ("SELECT * FROM Employees", $DB);

echo "$#@60;table border=1$#@62;\n";


echo "$#@60;TR$#@62;$#@60;TD$#@62; name $#@60;/td$#@62;$#@60;td$#@62; position $#@60;/tr$#@62;\n";

while ($myrow = Mysql_fetch_row ($result)) {

printf ("$#@60;tr$#@62;$#@60;td$#@62;%s%s$#@60;/td$#@62;$#@60;td$#@62;%s$#@60;/tr$#@62;\n", $myrow [1], $myrow [2], $myrow [3]);

}

echo "$#@60;/table$#@62;\n";

Include ("Footer.inc");

? $#@62;

Do you see what's going on? The contents of the header file are merged into the program, and PHP executes all the code. Notice how the $title is defined before the Header.inc header file is included. The code in HEADER.INC can access its value. This way, the title of the page is changed. Now that you can use the Header.inc header file in any program, all you have to do is to get the appropriate value for the $title variable in each main program.

header files, HTML, conditional decision statements, and looping statements, which are added to some, you can use the most concise code to write various complex programs of varying functions. When used in conjunction with a function, the header file is more effective, as we'll see later.

Next, we'll introduce the wonderful part: data validation.

Second, data verification

Imagine the situation: we've designed the database, and now ask the user to enter information to write to the database. Suppose you have a field that requires a numeric type of information, such as a price, and a cute user enters text information in this column, which causes the execution of your application to fail. The MySQL database refuses to accept the type of data you provide in the SQL statement, and presents you with a "solemn protest".

What do we do? You want to use data validation to prevent the above situation from happening.

Simply put, data validation means that we check the data (usually the user passes through an HTML form) to see if it complies with certain rules. Rules can be various, such as a data element cannot be empty, or require that the content of a data item must meet certain requirements (for example, in the previous example, the requirement must be a number rather than text, or the email address must contain a "@" word, etc.).

Data validation can be done either at the server or at the client. PHP is used for data validation at one end of the server, while javascript/"target=" _blank >javascript or other client-side scripting languages can provide data validation for the client. This article is about PHP, so we are here to focus on server-side checksums. If you want to find some ready-made, on the client run the data than the test procedures, then you can go to the Monkey Library to see.

To put the database aside for the time being, let's talk about the data validation method of PHP first. If you prefer (or you want to record the data we want to verify), you can add the other fields to the employee database that you built earlier, very simply by using the MySQL ALTER statement.

There are several PHP features that can be used to work with data validation, some simple and some more complicated. where strlen () is a relatively simple function that tells us the length of a variable.

A bit more complicated is ereg (), which can handle a complete regular expression for complex checksums. I don't want to talk too much about regular expressions, because a lot of books are written specifically about this problem. But I'll give some simple examples on the next page.

Let's start with a simple example. The following program checks to see if a variable exists.

$#@60;html$#@62 ;

$#@60;body$#@62;

$#@60;? PHP

if ($submit) {

if (! $first | |! $last) {


$error = "Sorry, you must fill in all the columns!" ";
} else {

//Process table entry

Echo Thank you! ";

}
}

if (! $submit | | | $error) {

echo $error;

? $#@62;

$#@60; p$#@62;

$#@60;form method= "POST" action= $#@60;? PHP echo $PHP _self $#@62; " $#@62;


First column: $#@60;input type= "text" name= "name value=" $#@60;? php echo $first? $#@62; " $#@62;$#@60;br$#@62;


Second column: $#@60;input type= "text" name= "surname" value= "$#@60;? php echo $last? $#@62; " $#@62;$#@60;br$#@62;

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

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

$#@60;? PHP


}//If End

$#@62;



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

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

The key place in this program is the nested conditional decision statement. The first level checks whether the user presses the button that sends the data. If so, the program then checks whether the $first and $last two variables exist. that | | Symbol denotes "or", and! The symbol denotes "non". The program is described in general language as "if the $first does not exist or the $last does not exist, then the $error variable is placed to the following value." ”

Next, we'll go further and check the length of the text. This is necessary to check the user's password because you do not want some lazy users to enter a password of only one or two words, which may require them to enter a six-bit long password.

We have already talked about the function of strlen (). It simply returns a number that is equal to the number of characters contained in the variable being measured. Here, I modify the above procedure, check the length of $first and $last.

$#@60;html$#@6 2;

$#@60;body$#@62;

$#@60;? PHP

if ($submit) {

if (strlen ($first) $#@60 6 | | | strlen ($LAST) $#@60 6) {


$error = "Sorry, you must fill out all Column! ";

} else {


//Processing table entry


echo "Thank you!" ";

}

}


if (! $submit | | $error) {

echo $error;

? $#@62;

$#@60; p$#@62;

$#@60;form method= "POST" action= $#@60;? PHP echo $PHP _self $#@62; " $#@62;


First column: $#@60;input type= "text" name= "name value=" $#@60;? php echo $first? $#@62; " $#@62;$#@60;br$#@62;


Second column: $#@60;input type= "text" name= "surname" value= "$#@60;? php echo $last? $#@62; " $#@62;$#@60;br$#@62;

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

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

$#@60;? PHP


}//If End

$#@62;


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

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

You can perform this program and enter six words or less than six words. This check is simple, but it works.



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.