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

Dynamically Insert and Update Values In a MySQL Database Using OOP

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

Time Is of the Essence

So you have a new project. It requires simple add, edit, and delete actions. Simple enough, but think of all the time you spend writing simple, time-consuming insert and update SQL statements, and praying to God that you didn't mess up the column sequence, forget a comma, or make some other silly type-o that you spend precious coding time looking for and correcting.

Think of the following: in a 25 or 50 field form, you have 50 or 100 pieces of data to enter into the SQL statement:


mysql_query("INSERT INTO myTBL (col1,col2,col3,…col30,col31) VALUES ('".$val1."', '".$val2."', '".$val3."', …'".$is_this_val30."', '".$please_be_val31."'");

It's confusing, and it makes your code look and feel like crap. It gets even worse when you have to add or delete a field, then you have to go through the SQL again and find that one column amidst the sea of endless columns and then go find that darned variable in that myriad of weird names you made up. Argh! I can hear your frustrations now. Well there is a solution to our long-SQL-statement-woes. 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.

Enough chit-chat, let's start coding.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: