Home arrow MySQL arrow Page 7 - Dynamically Insert and Update Values In a MySQL Database Using OOP

You Wouldn't Have to Update It Had You Gotten It Right the First Time - MySQL

Stop writing insert and update SQL statements and cut the time you spend writing simple SQL in half while focusing on the more complicated things. Leave it up to OOP to help you out. We will make a class that goes out and looks for the values for us and builds a SQL statement on the fly. All we have to do is make sure the column names in the database correspond with the field names in the HTML form. Believe me when I say it saves TONS of time. I never write applications that don't use it.

TABLE OF CONTENTS:
  1. Dynamically Insert and Update Values In a MySQL Database Using OOP
  2. Essential Connection
  3. Adam Up
  4. If You POST It, It Will Go
  5. Updateagenessly
  6. UpdateDB in Action
  7. You Wouldn't Have to Update It Had You Gotten It Right the First Time
By: Sam 'SammyK' Powers
Rating: starstarstarstarstar / 96
February 04, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The blob:


// Include the file that contains 
// the MyDatabase class
require('database.class.php');
 
// If the form was submitted...
if($_POST['id'])
{
 // Instantiate the class
 $SQL = new MyDatabase;
 // Connect to the database
 $SQL->Connect();


 // Update values in my_tbl 
 // where id = $_POST['id']
 if(!$SQL->UpdateDB('my_tbl', 
 $_POST['id'], 'id'))
  die( $SQL->GetError() );


 // Disconnect from the database
 $SQL->Disconnect();
}

Look familiar? It should, unless you accidentally / intentionally skipped a section or two. The only difference now is we are using the UpdateDB method to update our table and now we are passing the id and the name of the id column in the database table.

You Are Much Faster Now, Grasshopper

Now you are able to focus, grasshopper. No more annoying insert and update statements to distract you. This class has become a permanent "must have" in my arsenal of includes. It's like an audio stream from Digitally Imported Radio (www.di.fm) - I never code without it.

So there you have it; my 2 dollars. It is my hope that you will find this class as helpful as I have. Just don't use it in a production environment without adding some more validation and better error reporting and so on. Until next time!



 
 
>>> More MySQL Articles          >>> More By Sam 'SammyK' Powers
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: