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

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


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

Dev Shed Tutorial Topics: