PHP
  Home arrow PHP arrow Page 6 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 - Checkboxes and other HTML form processing


    (Page 6 of 6 )

    HTML forms are easy to design, as long as you allow one value per field. When you allow more than one value, the processing gets tricky.
    1. Checkboxes
    2. SELECT multiple scrolling lists
    3. Searching with multiple values

    Checkboxes

    Checkboxes are the simplest way to allow users to enter more than one value into a field:
    What pets do you have?
    Dog
    Cat
    Fish
    You can check one, two, or all of the pets. The HTML code looks like this:


    What pets do you have? <FORM> <INPUT TYPE=checkbox NAME=PET_ARRAY[] value=dog> Dog<br> <INPUT TYPE=checkbox NAME=PET_ARRAY[] value=cat> Cat<br> <INPUT TYPE=checkbox NAME=PET_ARRAY[] value=fish> Fish<br> </FORM>


    The MySQL field name is PET, but here we use PET_ARRAY[]. When the user clicks the SUBMIT button, the values are passed to the header looking like this:


    http://www.mywebsite.com/myform.php3?PET_ARRAY%5B%5D=dog&PET_ARRAY%5B%5D=cat


    5B is 91 in hexadecimal, and the HTML character entity for left square bracket — [ — is &#091, so %5B means left square bracket. 5D is 93 in hexadecimal, and the HTML character entity for right square bracket — ] — is &#093, so %5D means right square bracket.

    When we get to the PHP script that processes this form, we use this script to put both values into one field. The PET field is a SET datatype.


    if ($PET_ARRAY) { $PET = implode($PET_ARRAY, ","); $result = mysql_query ("UPDATE dbname SET PET = '$PET' "); if(!$result) { echo "<B>UPDATE unsuccessful:</b> ", mysql_error(); exit; } }


    if ($PET_ARRAY) checks if the user checked any of the boxes. If the user doesn't have any pets, the field is left empty.

    $PET = implode($PET_ARRAY, ","); converts the array into a string, with the elements separated by commas. The values passed to the header above would come out as


    dog,cat


    The query then puts the string into the PET of the database.

    To search a SET datatype, remember to put % wildcards before and after the search value. This is necessary to find one of several values, and ignore the commas. E.g.,


    SELECT * FROM dbname WHERE PET LIKE '%$PET%';



    SELECT multiple scrolling lists

    Another way to allow selection of more than one value is to use pull-down menus or scrolling lists. E.g.,
    What pets do you have?



    <FORM> What pets do you have? <FORM> <SELECT NAME=PET_ARRAY[] size=5 multiple> <option>Dog <option>Cat <option>Fish <option>Kangaroo <option>Ptarmigan <option>3-Toed Sloth <option>Lemur <option>Narwhal </select> </FORM>



    If you shift-click, you can select two or more adjacent values, e.g., Dog, Cat, and Fish. With Windows, you ctrl-click to select two or more non-adjacent values. On the Macintosh you hold down the butterfly key as you click your mouse.

    99.999% of Internet users don't know this, and 99.99% aren't going to read instructions you provide, so I don't use SELECT multiple scrolling lists. Checkboxes are more obvious.


    Searching with multiple values

    This is another short section. I haven't figured out how to search for more than one value, e.g., allow users to query a SET of pets to find people with a dog and a fish. If I figure it out I'll explain it here. {mospagebreak title=Using include() to bring in outside files} Sometimes you have a block of text that is repeated on many webpages. For example, all of these chapters start with "PHP and MySQL Website Database Basics" and the copyright, etc. Later you need to change one word, and you have to go into 20 documents to change the same word.

    A better way is to put the block of text into its own document, and use include() to bring the text into each webpage. Any change you make in the text document will appear in all your webpages.

    1. include() with text
    2. include() with applets
    3. Using <object>

    include() withtext

    Write the text object as a PHP file, e.g.,


    <?php print "This is my text block."; ?>


    Save it as a PHP files, e.g., textblock.php3.

    In each webpage, you put in this PHP function:


    include ('textblock.php');



    include() with applets

    include() can do more than text. Any PHP file can be included. You should be able to call other types of files, but I haven't tried this.


    Using <object>

    HTML's code <object> is another way to include external files. It was created for applets, such as Java and Microsoft's ActiveX, but supposedly works with other types of files. I've never gotten it to work.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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