FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
FORMfields v3 Tutorials
FORMfields Tutorials
FORMfields Lib Installation
FORMgen Installation
Manually Creating a Form
Load/Unload From a DB
Emailing a Form
Forms on Your Webpage
Creating Tables
Pagifying Data
Using CSS to Format Forms 1
Using CSS to Format Forms 2
Other Tutorials
Loading/Unloading Values From a Database
Last Updated: September 18th, 2007
Overview
Using the form structure from the tutorial, Manually Creating a Form, we will demonstrate how to quickly load and unload data from a database.
Creating Our Example Database Table
First we use phpMyAdmin or the MySQL client, to execute the following SQL code:

    CREATE TABLE `ffce_customers` (
      `customer_id` int(10) unsigned NOT NULL auto_increment,
      `name` varchar(40) NOT NULL default '',
      `email` varchar(50) NOT NULL default '',
      `phone` varchar(15) NOT NULL default '',
      `fax` int(10) unsigned NOT NULL default '0',
      PRIMARY KEY  (`customer_id`)
    );
    
Inserting Form Data Into a Database
As long as the field names in our database match the names of the attributes in our database, we can insert the data from the entire form with just one function call:

    <?
        
// Insert the all the form data into the table ffce_customers
        
$ffForm->insertValuesIntoDb("ffce_customers");
    
?>
    
Click here to download the source code for the full example.
Selecting Form Data from a Database
Once again, with just one function call we can retrieve an entire form's worth of data from a database. Here's how:

    <?
        $ffForm
->loadValuesFromDb("ffce_customers""WHERE customer_id='1'");
    
?>
    
The 2nd parameter, or where clause, specifies how our form data is uniquely identified.

Click here to download the source code for the full example.
Updating Form Data in a Database
With another function call, we can update an entire form's worth of data in our database:

    <?
        $ffForm
->updateValuesInDb("ffce_customers""WHERE customer_id='1'");
    
?>
    
Click here to download the source code for the full example.
Deleting Form Data from a Database
With a single function call, we can remove an entire form's worth of data from our database:

    <?
        $ffDb 
= &FfDb::s();
        
$ffDb->query("DELETE FROM ffce_customers WHERE customer_id='1'");
    
?>
    
Sometimes web designers create a separate file for deleting records. In this method you could create a file like the following that could be used to delete an individual record. And for example, to delete record 2, you could link to deleteRecord.php?id=2.

deleteRecord.php:
<?
    $ffDb 
= &FfDb::s();
    
$ffDb->query("DELETE FROM ffce_customers WHERE customer_id='" $_REQUEST["id"] . "'");
?>
    
More Complex Database Operations
Sometimes the above methods are not enough for performing more complex database operations as we want more control over the database queries.
Manual Select
Here is an example of how to manually select data from a database:

    <?
        $ffDb 
= &FfDb::s();
        
$values 
            
$ffDb->getSimpleValues(
                
"SELECT name, email, phone, fax "
                
"FROM ffce_customers "
                
"WHERE customer_id='1'"
            
);
        
$ffForm->loadDbValues($values);
    
?>
    
Or if we want even more control, we can set each form field value individually, like:

    <?
        $ffDb 
= &FfDb::s();
        
$rs 
            
$ffDb->query(
                
"SELECT name, email, phone, fax "
                
"FROM ffce_customers "
                
"WHERE customer_id='1'"
            
);
        
$values mysql_fetch_array($rs);
        
$ffForm->setDbValue("name"$values["name"]);
        
$ffForm->setDbValue("email"$values["email"]);
        
$ffForm->setDbValue("phone"$values["phone"]);
        
$ffForm->setDbValue("fax"$values["fax"]);
    
?>
    
Click here to download the source code for the full example.
Manual Update
Likewise, we can choose to manually update the form data with:

    <?
        $ffDb 
= &FfDb::s();
        
$ffDb->query(
            
"UPDATE ffce_customers "
            
"SET "
                
"name=" $ffForm->getDbValue("name") . ","
                
"email=" $ffForm->getDbValue("email") . ","
                
"phone=" $ffForm->getDbValue("phone") . ","
                
"fax=" $ffForm->getDbValue("fax") . " "
            
"WHERE customer_id='1'"
        
);
    
?>
    
Note: getDbValue() returns an escaped string that has been prepared for use in a SQL query.

Click here to download the source code for the full example.

As you can see, FORMfields gives you the choice to perform queries manually and automatically!
Info
Next Tutorial: Emailing a Form »
Support | Earn Money
Copyright © 2005-2018 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.