PHP and MySQL basic tutorial (2)

Source: Internet
Author: User
Tags ereg
PHP and MySQL basic tutorial (2). Read the PHP and MySQL basic tutorial (2). The data validation clearing space trim function will clear spaces at the beginning and end of the data. Its syntax is: trim ($ first_name); processing of required fields is required for some fields in your database. In this way, fields corresponding to the HTML form cannot be blank. "> <LINKhr data verification

Clear spaces

The trim function will clear spaces at the beginning and end of the data. Its syntax is:

Trim ($ first_name );


Processing of required fields

Some fields must be entered in your database. In this way, the fields corresponding to the HTML form cannot be blank. Of course, this verification process can be handled using the client's JavaScript script, but since we are talking about PHP, we should still use PHP for processing. The following code checks whether the user's last name is entered:

If (ereg (".", $ first_name) = 1)

{

Print ("surname :");

Print ("$ first_name ");

$ Verify = "OK ";

}

Else

{

Print ("<B> error: </B> your surname is not filled ");

$ Verify = "bad ";

}

The ereg pattern recognition function is used to determine whether a specified string contains a substring. Its first parameter is to determine whether it contains a substring. The second parameter specifies the string to be searched, usually a variable. The Ereg function returns "0" (false), indicating that the matching fails, or "1" (true), indicating that the matching is successful. Here, "." is a pattern-type unified character, which represents any character. In this way, the expression ereg (".", $ first_name) = 1 means that the variable $ first_name contains at least one character.

Check the email address
Prepared by: Yangmei compilation clicks in this article: 118

Using the following character constants as the first parameter of the ereg function, you can easily check the e-mail address:

"@": Must contain @

"^ @": @ Cannot be used for header hitting.

"@. *...": A character must exist between @ and.

"... *": It must contain at least two characters after.

"": Spaces are not allowed.

  

Based on these Parameter examples, you can also design other input verification methods.

Check whether the user name is unique

This action also seems necessary:

Mysql_connect (localhost, username, password );



Mysql_select_db (dbname );

$ Result = mysql_query ("SELECT * FROM tablename

WHERE USER_ID = '$ USER_ID'

");

If ($ row = mysql_fetch_array ($ result ))

{

Print ("<B> error: </B> user name <B> ");

Print ("$ USER_ID ");

Print ("</B> is in use. select another one and try again. ");

Print ("<p> ");

$ Verify = "bad ";

}

Else

{

Print ("User ID :");

Print ("$ USER_ID ");

}

The idea of the code is very simple. I believe it is hard for you to read it here.
Check whether the user name is unique
Prepared by: Yangmei compilation clicks in this article: 118

This action also seems necessary:

Mysql_connect (localhost, username, password );


Mysql_select_db (dbname );

$ Result = mysql_query ("SELECT * FROM tablename

WHERE USER_ID = '$ USER_ID'

");

If ($ row = mysql_fetch_array ($ result ))

{

Print ("<B> error: </B> user name <B> ");

Print ("$ USER_ID ");

Print ("</B> is in use. select another one and try again. ");

Print ("<p> ");

$ Verify = "bad ";

}

Else

{

Print ("User ID :");

Print ("$ USER_ID ");

}

The idea of the code is very simple. I believe it is hard for you to read it here.

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.