PHP and MySQL Basics Tutorial (ii)

Source: Internet
Author: User
Tags ereg html form mail mysql php and php and mysql variable trim
mysql| Basic Tutorial Data validation

Clean space

The TRIM function clears the trailing space of the data, and its syntax is:

Trim ($first _name);


Processing of required fields

In your database, some fields must be filled in. The corresponding fields corresponding to the HTML form are not allowed to be empty or filled out. Of course, this validation process can be handled using the client's JavaScript script, but since we're talking about PHP, we're going to use PHP to handle it. The following code checks to see if the user's last name is entered:

if (Ereg (".", $first _name) = = 1)

{

Print ("Last Name:");

Print ("$first _name");

$verify = "OK";

}

Else

{

Print ("< b> error: </b> your last name is not filled");

$verify = "bad";

}

A ereg pattern recognition function that determines whether a specified string contains a substring. Its first argument is to decide whether to include substrings, and the second parameter specifies the string to search for, usually a variable. The Ereg function returns "0" (false) to indicate a match failure, or "1" (true) to indicate a successful match. Here's a comma. " "is a pattern of other wildcard characters that represent any character." The Expression ereg (".", $first _name) = = 1 means that at least one character is included in the variable $first _name.

Check e-mail address

Use the following characters constants as the first parameter of the Ereg function, you can easily check the e-mail address:

' @ ': Must contain @

"^@": Cannot begin with @

"@.*": at @ and. There must be characters in the middle.

"... *": In. And then at least two characters.

"": No Spaces allowed

  

You can also design some of the other input validations, as compared to these several parameter examples.

Check that the user name is unique

This action seems to have to be done too:

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> username < b>");

Print ("$USER _id");

Print ("</b> is already occupied, please select others to try again." ");

Print ("< p>");

$verify = "bad";

}

Else

{

Print ("User ID:");

Print ("$USER _id");

}

The idea of the code is simple, read here, believe it has been hard to live with you.
Check that the user name is unique

This action seems to have to be done too:

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> username < b>");

Print ("$USER _id");

Print ("</b> is already occupied, please select others to try again." ");

Print ("< p>");

$verify = "bad";

}

Else

{

Print ("User ID:");

Print ("$USER _id");

}



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.