PHP
  Home arrow PHP arrow Page 3 - Website Database Basics With PHP and M...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Website Database Basics With PHP and MySQL
By: Thomas Kehoe
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 57
    2000-01-11

    Table of Contents:
  • Website Database Basics With PHP and MySQL
  • HTML talks to PHP talks to MySQL
  • Verifying form data
  • Using cookies to identify and track visitors
  • Weird SQL: What The Books Don't Tell You
  • Checkboxes and other HTML form processing

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Website Database Basics With PHP and MySQL - Verifying form data


    (Page 3 of 6 )

    Some visitors to your website will try to enter invalid data into your database. You'll want to write a verification script to clean up entered data.
    1. Trimming white space
    2. Required fields
    3. Checking e-mail addresses
    4. Checking that the username is unique

    Trimming white space

    trim will remove white spaces from the beginning and end of the data:

    trim ($first_name);


    Required fields

    Some of your fields will be required. In other words, visitors have to enter something in the field. The following script checks that a first name was entered:



    if (ereg(".", $first_name) == 1) { echo "First name: ", "$first_name"; $verify = "OK"; } else { print ("<b>Error:</b> A first name is required."); $verify = "bad"; }



    ereg means "evaluate regular expression". "Regular expressions" are the UNIX function for finding patterns in strings of letters and numbers. ereg is followed by parentheses, and you can put three arguments in the parentheses. The arguments are separated by commas. The first argument is the pattern to search for, usually surrounded by quotation marks. The second argument is where ereg is to search, usually a variable. The third, optional, argument is an array to put matches into. This argument is a variable.

    ereg returns either a "0" (false) or a "1" (true).

    The dot . or period is a regular expression wild card meaning "any character."

    (ereg(".", $first_name) == 1) means "the variable '$first_name' contains anything". If this expression is true, then the first name is printed, and the variable $verify is set to "OK".

    The else argument executes when ereg returns "0" (false).

    There are three other versions of the ereg function.

    1. ereg_replace uses three arguments: the first is the pattern to search for, the second is the pattern to replace the first pattern, and the third is where to search (a variable).
    2. eregi is the same as ereg, except that it's not case-sensitive (i.e., it doesn't differentiate upper- and lower-case letters).
    3. eregi_replace is not case sensitive
    Alternative means to the same end

    The line if (ereg(".", $first_name) == 1) can be simplified to if ($first_name). I used the longer form to show how to use ereg in a simple example.

    Checking e-mail addresses

    The following ereg arguments test validity of e-mail addresses:

    "\@"Must include @
    "^\@"Can't begin with @
    "\@.*.\."Must have characters between @ and .
    "\....*"At least two characters after the .
    " "No spaces permitted
    "<" ">"No angle brackets permitted

    Checking that the username is unique

    You may need to make sure no two visitors try to use the same name:



    mysql_connect (localhost, username, password); mysql_select_db (dbname); $result = mysql_query ("SELECT * FROM tablename WHERE USER_NAME = '$USER_NAME' "); if ($row = mysql_fetch_array($result)) { print ("<b>Error:</b> The user name <b>"); print ("$USER_NAME"); print ("</b> has been taken. Go back and enter a new user name."); print ("<p>"); $verify = "bad"; } else { print ("User name: "); print ("$USER_NAME"); }



    This code connects to MySQL, then searches the database for the entered username. Note that the = equals sign is used for an exact search, when previously we used LIKE to do wildcard searches. If any result is found, the visitor is told to enter a new username.

    More PHP Articles
    More By Thomas Kehoe


       · I love your article. And i think everybody should read it.
       · Thanks for your time in writing this article. I was able to easily implement most...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT