DB Forms: PHP, MySQL, and PHPLIB
by Darryl Ross and Con Zymaris


Example 1:
(a)
mysql -u <user with create table privileges> <database> < <sql file>
e.g. mysql -u root test < example.sql

(b)
create table address(
    name varchar(50) not null,
    address varchar(100),
    city varchar(50),
    state varchar(50),
    postcode int,
    phone varchar(20),
    fax varchar(20),
    email varchar(20),
    primary key(name)
    );

Example 2:
GRANT SELECT, DELETE, INSERT, UPDATE ON test.address TO webuser@localhost
IDENTIFIED BY 'testing';


Listing One
<html>
    <head>
        <title>My First PHP Script</title>
    </head>
    <body>
        <? echo "Hello World! (When you're on a good thing...)"; ?>
    </body>
</html>  

Listing Two
<?php
  # Include the class to handle database connections and queries (from PHPLIB)
  include("db_mysql.inc");
  # Extend the database class and overide the connection parameters
  class DB_Example extends DB_Sql {
    var $Host     = "localhost";              
    var $Database = "test";            
    var $User     = "webuser";                  
    var $Password = "testing";                     
  }
  $db = new DB_Example;
  $db->query("SELECT * FROM address");
  while ($db->next_record()) {
    printf("%s %s<br>\n", $db->f("name"), $db->f("phone"));
  }
?>

Listing Three
<?php  
  require("oohforms.inc");         # include the library
  $f = new form;                   # create a form object
  # set up form elements
  $f->add_element(array("type"=>"text", "name"=>"foo", 
                        "valid_regex"=>"^[a-z]*$",
                        "valid_e"=>"Letters only", "icase"=>1, 
                        "value"=>"bar"));     
  $f->add_element(array("type"=>"submit", "name"=>"submitname"));
  if ($submitname)                 # Is there data to process?
    if ($err = $f->validate()) {   # Is the data valid?
      echo $err;                   # No; Display error
      $f->load_defaults();         # Load form with submitted data
    }  
    else {
      printf("<b>%s</b><br>\n", $foo);         # Data ok; Do something with it
    }
  $f->start("example");                        # Start displaying form
  # Show elements
  $f->show_element("foo");    
  $f->show_element("submitname", "submit");    # Show elements
  $f->finish();                     # Finish form
?>

Listing Four
<?php
  # Include the class to handle database connections and queries (from PHPLIB)
  include("db_mysql.inc");
  # Include the DBF_FormEdit class
  include("dbf_form.inc");
  include("dbf_form_edit.inc");

  # Extend the database class and overide the connection parameters
  class DB_Example extends DB_Sql {
    var $Host     = "localhost";              
    var $Database = "test";            
    var $User     = "webuser";                  
    var $Password = "testing";                     
  }
  # Extend the DBF edit class and overide the database class
  class DBE_Example extends DBF_Edit {
    var $database_class = "DB_Example";
  }
  # Extend the DBF form edit class
  class DBF_Example extends DBF_FormEdit {
    var $dbf_edit_class = "DBE_Example";    # Which dbf_edit class to use
  }  
  # Create an instance of the DBF edit class
  $dbf = new DBF_Example;
  $dbf->init(); # set up the DBF_FormEdit object

  # Add table and the primary key
  $dbf->add_table(array("name"=>"address", "key"=>array("name")));

  # Add fields
  $dbf->add_field(array("type"=>"text","name"=>"name"));
  $dbf->add_field(array("type"=>"text","name"=>"address"));
  $dbf->add_field(array("type"=>"text","name"=>"city"));
  $dbf->add_field(array("type"=>"text","name"=>"state"));
  $dbf->add_field(array("type"=>"text","name"=>"postcode"));
  $dbf->add_field(array("type"=>"text","name"=>"phone"));
  $dbf->add_field(array("type"=>"text","name"=>"email"));

  # Add a form element
  $dbf->add_element(array("type"=>"submit","name"=>"submit"));
  $dbf->add_element(array("type"=>"submit","name"=>"delete"));

  print("<html>\n");
  print("  <body>\n");

  if ($submit) { # Check if there was a submission 
    # Save the form data to the database table
    $dbf->save();
  }
  else if ($delete) {
    # delete the record from the database 
    $dbf->delete();
  }
  else {
    # Load form data with data from the database table
    # Requires the primary key field(s) to have valid values
    # Calls DBF_Edit load_defaults()
    $dbf->dbf_load_defaults(); 
  }
  # Calls form load_defaults(), this will load defaults for all for elements.
  $dbf->load_defaults(); 

  $dbf->start();
  $dbf->dbf_show_elements();
  $dbf->show_element("submit","Save");
  $dbf->show_element("delete","Delete");
  $dbf->finish();
  print("  </body>\n");
  print("</html>\n");
?>

Listing Five
# Extend the database class and overide the connection parameters
class DB_Example extends DB_Sql {
  var $Host     = "localhost";              
  var $Database = "test";            
  var $User     = "webuser";                  
  var $Password = "testing";                     
}

Listing Six
# Create an instance of the DBF edit class
$dbf = new DBF_Example;
$dbf->init(); # set up the DBF_FormEdit object  

Listing Seven
# Add table and the primary key
$dbf->add_table(array("name"=>"address", "key"=>array("name")));

Listing Eight
# Add fields
$dbf->add_field(array("type"=>"text","name"=>"name"));
$dbf->add_field(array("type"=>"text","name"=>"address"));
  ...

Listing Nine
if ($submit) { # Check if there was a submission 
  # Save the form data to the database table
  $dbf->save();
}
else if ($delete) {
  # delete the record from the database 
  $dbf->delete();
}
else {
  # Load form data with data from the database table
  # Requires the primary key field(s) to have valid values
  # Calls DBF_Edit load_defaults()
  $dbf->dbf_load_defaults(); 
}

4


