PHP and MySQL Basic Tutorial (ii) _php tutorial

Source: Internet
Author: User
Data validation

Clean up spaces

The TRIM function clears the space at the ends 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. Of course, this verification process can be handled by 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 whether 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 name not been filled in");

$verify = "bad";

}

The Ereg pattern recognition function is used to determine whether a specified string contains a substring. Its first parameter is the substring that determines whether it is contained, the second parameter specifies the string to search for, usually a variable. The Ereg function returns "0" (false), indicating that the match failed, or "1" (true), indicating a successful match. Here's a comma. " "is a pattern-type wildcard character that represents any of the characters. The Expression ereg (".", $first _name) = = 1 means that at least one character is included in the variable $first _name.

Check e-mail address
Author: Yang Eyebrow compiling this article click: 118

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

' @ ': Must contain @

"^@": Can't begin with @

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

"..... *": In. Must be at least two characters later

"": No Spaces allowed

  

You can also design some other input validation against these examples of these parameters.

Check that the user name is unique

This action seems to have to be done as well:

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 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 very simple, read here, I believe it is hard to live with you.
Check that the user name is unique
Author: Yang Eyebrow compiling this article click: 118

This action seems to have to be done as well:

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 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 very simple, read here, I believe it is hard to live with you.

http://www.bkjia.com/PHPjc/315117.html www.bkjia.com true http://www.bkjia.com/PHPjc/315117.html techarticle The data validation cleanup space trim function clears the whitespace of the data, and its syntax is: Trim ($first _name); Required fields are processed in your database, some fields must be filled in. ...

  • 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.