WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Simple class to create insert and update statements. Independent of the access to the database. Makes handling complex inserts easier - especially when the table structure is liable to change.
Categories : Databases, PHP Classes, PHP Update Picture
Sarah King
Date : Mar 15th 2002
Grade : 1 of 5 (graded 1 times)
Viewed : 6268
File : cl_insert.php
Images : No Images for this code example.
Search : More code by Sarah King
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
////////////////////////////////////////////////////////////
// CreateSQL 0.1
// class for creating SQL statements independant of the database
// not always appropriate to create the SQL within the db class
// Makes complex inserts easier to manage - visually easier editing
//
// Sarah King
// sarah@pcpropertymanager.com
// http://www.pcpropertymanager.com
//
//
//         $sql = new CreateSQL("mytable","mytableid");
//         $sql->set_fields( array( "name" => $username,
//         "email" => $email,
//         "phone" => $tel,
//         "fax" => $fax,
//         "address" => $address,
//         "postcode" => $postcode,
//         "country" => $country,
//         ));                 
//
// then use database class to run the query
// $db->query($sql->getUpdateSQL());
//
////////////////////////////////////////////////////////////


if(!defined("__CreateSQL__")) {
define("__CreateSQL__",1);

class CreateSQL
{
/**************************************************************************
********
Create Insert and update clauses based on data passed.
***************************************************************************
*******/

        var $aFields = array();
        var $tablename;
        var $primarykey;
        var $primarykeyvalue;
        var $whereClause = "";

        function CreateSQL($tablename, $primarykey, $primarykeyvalue = "NULL")        //
constructor
        {
                $this->tablename = $tablename;
                $this->primarykey = $primarykey;
                $this->primarykeyvalue = $primarykeyvalue;
        }
        
function set_fields($fieldname, $fieldvalue = "") {
if (!is_array($fieldname)) {
if ($fieldvalue == "") {
$fieldvalue = "NULL";
}
$this->aFields[$fieldname] = $fieldvalue;
} else {
reset($fieldname);
while(list($f, $v) = each($fieldname)) {
$this->aFields[$f] = $v;
}
}
}

function setPrimaryKey($keyval){
$this->primarykeyvalue = $keyval;
} //setPrimaryKeyVal

function setWhere($where){
$this->whereClause .= $where;
}

        function getInsertSQL()
        {
$output = "insert into " . $this->tablename . " (";
         $fieldnames = "";
         $fieldvals = "";

reset($this->aFields);
         while(list($f, $v) = each($this->aFields)) {
$fieldnames .= $f . ", ";
                 $fieldvals .= "'" . addslashes($v) . "', ";
}
         $fieldnames = substr($fieldnames, 0, -2);
         $fieldvals = substr($fieldvals, 0, -2);

         $output .= $fieldnames . ") values (" .$fieldvals . ")";
         return $output;

        }// getInsertSQL

        function getUpdateSQL()
        {
$output = "update " . $this->tablename . " set ";
         $vars = "";
reset($this->aFields);
         while(list($f, $v) = each($this->aFields)) {
$vars .= "$f = '" . addslashes($v) . "', ";
}
         $output .= substr($vars, 0, -2);

         if ($this->primarykeyvalue != "NULL"){
         //MySQL doesn't object to quotes around numbers
$output .= " where " . $this->primarykey . " = '" . $this-
>primarykeyvalue . "'";
                 $bitstr = " and ";
         }
         else $bitstr = " where ";
         if (!empty($this->whereClause)) $output .= $bitstr . $this->whereClause;

         return $output;

        }// getUpdateSQL

} // class CreateSQL
}// end of definition statement
?>



Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0.
Categories : Databases, PHP, mSQL, Databases
Use this class to connect your database transparently...
Categories : PHP Classes, Databases, PHP
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser!
Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
Recordset Class for MSSQL database
Categories : PHP Classes, Databases, PHP, MS SQL Server
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL