I have made a basic class that can take values from a form post, these can be in any combination and to any table and it will post the new record to the database.
Note: i have some validation functions i have not yet added.
the call on the php side
the class
Note: i have some validation functions i have not yet added.
the call on the php side
PHP:
/************************* Post record *******************************/
// Posts the record to the database
$post = new newrecord;
// Tbl Name
$post->tbl_name = "forum-posts";
// Send variables
$post->postid = $_POST['postid']; // this is only used if it is something like a comment that relates to another object
$post->subject = $_POST['subject'];
$post->body = $_POST['body'];
// Display output
$post->PostToDatabase();
the class
PHP:
<?php
/************************************************************************************
Jerry Roy
KittenbunnyCore
Database Class: Add new record
************************************************************************************/
// Prevent direct access to file //
if(eregi(basename(__FILE__),$_SERVER['REQUEST_URI']))
die('<h1>Forbidden</h1><p>Direct access prohibited.</p>');
// Class deals with the following components
// --- Tbl --- //
// tbl_name
// --- DATA --- //
// PostID (relates for forum replys)
// Subject
// Body
// Date
// Poster
class newrecord
{
// Variables
var $tbl_name;
var $postid;
var $subject;
var $body;
var $date;
var $poster;
// Validate the Tbl_name
function tbl_name()
{
if (isset($this->tbl_name))
{
return ' `'.$this->tbl_name.'`';
}
}
// Validate the PostID
function ValidatePostID()
{
if (isset($this->postid))
{
$valid_postid = $this->postid;
return $valid_postid;
}
}
// Validate the subject
function ValidateSubject()
{
if (isset($this->subject))
{
return $this->subject;
}
}
// Validate the Body
function ValidateBody()
{
if (isset($this->body))
{
return $this->body;
}
}
// Validate the Date
function ValidateDate()
{
if (isset($this->date))
{
return $this->date;
}
}
// Validate the Poster
function ValidatePoster()
{
if (isset($this->poster))
{
return $this->poster;
}
}
// Make the sql string
function MakeSQL()
{
// Varibles
// Feilds
$field['0'] = '`post_id`';
$field['1'] = '`subject`';
$field['2'] = '`body`';
$field['3'] = '`date`';
$field['4'] = '`poster`';
// Values
$value['0'] = $this->ValidatePostID();
$value['1'] = $this->ValidateSubject();
$value['2'] = $this->ValidateBody();
$value['3'] = $this->ValidateDate();
$value['4'] = $this->ValidatePoster();
//******************** INSERT string *******************************//
// Build the array
$x = 0;
foreach ($field as $key)
{
if (isset($value[$x]))
{
$strArray[$x] = $field[$x];
}
$x++;
}
// Build the array
$queryStr = '';
$andStr = ', ';
// Build the string
foreach($strArray as $str)
{
if(!empty($str))
{
$queryStr = $queryStr . $str . $andStr;
}
}
// Remove last $andStr added to str
$queryStr = substr($queryStr, 0, strlen($queryStr)-strlen($andStr));
// Build the query
// Start the query
$start_return = '$sql = sprintf("INSERT INTO '.$this->tbl_name().'';
// tables
$field_return = ' ('.$queryStr.') ';
//******************** INSERT string *******************************//
//******************** Value string *******************************//
// Values
// Build the array
$x = 0;
foreach ($field as $key)
{
if (isset($value[$x]))
{
$strArray[$x] = '%s';
}
$x++;
}
$queryStr = '';
$andStr = ', ';
// Build the string
foreach($strArray as $str)
{
if(!empty($str))
{
$queryStr = $queryStr . $str . $andStr;
}
}
// Remove last $andStr added to str
$queryStr = substr($queryStr, 0, strlen($queryStr)-strlen($andStr));
$values_return = 'VALUES ('.$queryStr.')", ';
//******************** Value string *******************************//
//******************** Data String ********************************//
$x = 0;
foreach ($field as $key)
{
if (isset($value[$x]))
{
$strArray[$x] = mysql_real_escape_string($value[$x]);
}
$x++;
}
// Build the array
$queryStr = '';
$andStr = ', ';
// Build the string
foreach($strArray as $str)
{
if(!empty($str))
{
$queryStr = $queryStr . $str . $andStr;
}
}
// Remove last $andStr added to str
$queryStr = substr($queryStr, 0, strlen($queryStr)-strlen($andStr));
$input_return = $queryStr;
//******************** Data String ********************************//
//Close
$close_return = ');';
// Build up the SQL String
return $start_return . $field_return . $values_return . $input_return . $close_return;
}
// Return the results
function PostToDatabase()
{
$query = mysql_query($this->MakeSQL());
if(!$query)
{
echo "There was a error entering the data into the database, please report this issue to the site admin.";
}
else
{
echo "The post was successful";
}
}
}
?>