Php_mysql Tutorial-Third day basic function 1th/2 page _php Basics

Source: Internet
Author: User
Tags html form require strlen
First page 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.
Copy Code code as follows:

<body>
<?php
Include ("Emptyfile.inc");
echo "Hello World";
?>
</body>

Web Page Production | website Construction | data collection.
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.
Copy Code code as follows:

<?php
$db = mysql_connect ("localhost", "root");
mysql_select_db ("MyDB", $db);
?>
<title>
<?php Echo $title?>
</title>
<body>
<center>
Very comprehensive site of a PHP technology, there are quite a lot of articles and source code.
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.
Copy Code code as follows:

<?php
$title = "Hello World";
Include ("Header.inc");
$result = mysql_query ("SELECT * FROM Employees", $DB);
echo "<table border=1>n";
echo "<tr><td> name </td><td> position </tr>n";
while ($myrow = Mysql_fetch_row ($result)) {
printf ("<tr><td>%s%s</td><td>%s</tr>n", $myrow [1], $myrow [2], $myrow [3]);
}
echo "</table>n";
Include ("Footer.inc");
?>

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 page data validation
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 or other client script programming 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.
Copy Code code as follows:

<body>
<?php
if ($submit) {
if (! $first | |! $last) {
$error = "Sorry, you must fill in all the columns!" ";
} else {
Working with form entries
echo "Thank you!" ";
}
}
if (! $submit | | $error) {
Echo $error;
?>
<P>
<form method= "POST" action= "<?php echo $PHP _self?>" >
First column: <input type= "text" name= "name" value= "<?php echo $first?>" ><br> second column: <input type= "text" Name= "last Name "Value=" <?php echo $last?> "><br> <input type=" Submit "name=" submit "value=" Input Information ">
</form>
<?php
}//If End
?>
</body>
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.
Copy Code code as follows:

<body>
<?php
if ($submit) {
if (strlen ($first) < 6 | | strlen ($LAST) < 6) {
$error = "Sorry, you must fill in all columns!" ";
} else {
Working with form entries
echo "Thank you!" ";
}
}
if (! $submit | | $error) {
Echo $error;
?>
<P>
<form method= "POST" action= "<?php echo $PHP _self?>" >
First column: <input type= "text" name= "name" value= "<?php echo $first?>" ><br> second column: <input type= "text" Name= "last Name "Value=" <?php echo $last?> "><br> <input type=" Submit "name=" submit "value=" Input Information ">
</form>
<?php
}//If End
?>
</body>

You can perform this program and enter six words or less than six words. This check is simple, but it works. >>
Current 1/2 page 12 Next read the full text

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.