Location: PHPKode > scripts > phpFormBuilder > php-form-builder-2010-02-01/Database.php
<?php
/**
*
* @package luc
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @author Emily Brand
* 
* Minimum Requirement: PHP 5.2
*/
require_once('constants.class.php');
require_once('fieldstructure.class.php');

class Database {

  public $conn;   //Used to keep the connection open

  /**
   * Connect to the database using the constants class's values and save the connection.
   */
  function Database(){
    global $constants;
    $this->conn = mysql_connect($constants->db_ip, $constants->db_user, $constants->db_pass);
    mysql_select_db($constants->db_name, $this->conn) or die("There was an error connecting to the database. Please contact the administrator or check back later.");
    $database->conn = $this->conn;
  }
  
  /**
   * The base function for creating forms. It checks to see if the table exists
   * and that all of the necessary columns are in the table. It also directs to
   * the function to add the data to the database.
   * @param $tablename
   * @param $tablestructure
   * @param $values
   * @param $content
   * @return unknown_type
   */
  function insertIntoDatabase($tablename, $tablestructure, $values, $content) {
    $result = mysql_query("SHOW COLUMNS FROM {$tablename}", $this->conn);
    if(!$result) {
      return ($this->createTable($tablename, $tablestructure)) ? $this->insertForm($tablename, $values, $content) : false;
    }
    $cols = array();
    while($obj = mysql_fetch_object($result)) {
      if(array_key_exists($obj->Field, $tablestructure)) {
        unset($tablestructure[$obj->Field]);
      }
    }
    if(array_count_values($tablestructure)) {
      $this->updateTable($tablename, $tablestructure);
    }
    return $this->insertForm($tablename, $values, $content);
  }
  
  /**
   * Create a table with the specified name and fields.
   * @param $tablename
   * @param $tablestructure
   * @return true or false
   */
  function createTable($tablename, $tablestructure) {
    // Begin creating the table.
    $q = "CREATE TABLE {$tablename} (";
    
    // Always create an autoincrement id field
    $q .= "id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, ";
    
    // Go through the values to add each one to the table.
    foreach($tablestructure as $k => $field) {      
      $q .= "{$field->getId()} {$field->getType()} {$field->getSize()}, ";
    }
    $q .= "contenthtml MEDIUMTEXT);";
    return mysql_query($q, $this->conn);
  }
  
  /**
   * Add any new columns that were not created with the original table,
   * but have been added to the form recently.
   * @param $tablename
   * @param $tablestructure
   * @return true or false
   */
  function updateTable($tablename, $tablestructure) {
    // Begin creating the table.
    $q = "";
    
    // Go through the values to add each one to the end of the table.
    foreach($tablestructure as $k => $field) {      
      $q .= "ALTER TABLE {$tablename} ADD {$field->getId()} {$field->getType()} {$field->getSize()};";
      mysql_query($q, $this->conn);      
    }
    return true;
  }
  
  function insertForm($tablename, $values, $content) {
    $q = "INSERT INTO {$tablename} SET ";
    foreach($values as $id => $value) {
      $q .= "{$id} = '{$value}', ";
    }
    $q .= "contenthtml = '".mysql_real_escape_string($content)."';";
    return mysql_query($q, $this->conn);    
  }

}

$database = new Database();
Return current item: phpFormBuilder