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

If You POST It, It Will Go - 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.

  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



So now all you have to do is name your form fields the same as your database columns and you are (as we say in Kentucky,) "in bidnus." Let's create an example MySQL table:

CREATE TABLE `my_tbl` (
`idint(5NOT NULL auto_increment,
`namevarchar(100NOT NULL default '',
`ageint(3NOT NULL default '0',
`genderenum('MALE','FEMALE'NOT NULL default 'MALE',
`phonevarchar(50NOT NULL default '',
`date_addeddatetime NOT NULL default '0000-00-00 00:00:00',

Now we just create an HTML form using the POST method. We name the fields the same names as the columns in my_tbl.

<form action="<?php echo $_SERVER['PHP_SELF']; ? >" method="post" name="myForm">
<p>Name: <input name="name" type="text"></p>
<p>Age: <input name="age" type="text"></p>
<input type="radio" name="gender" value="MALE"Male<br>
<input type="radio" name="gender" value="FEMALE"Female</p>
<p>Phone Number: <input name="phone" type="text"></p>
<p><input name="action" type="submit" value="AddToDB"></p>

The rest is really simple.

// Include the file that contains
// the MyDatabase class
// If the form was submitted...
 // Instantiate the class
 $SQL = new MyDatabase;
 // Connect to the database
 // To get a datetime stamp in the
 // date_added column
 $_POST['date_added'] = 'DATESTAMP';
 // Add values to my_tbl
  die( $SQL->GetError() );
 // Disconnect from the database

Check to see if the form has been submitted, instantiate the class, connect to the database, execute AddToDB passing the table name to it, and finally disconnecting from the database.

die( $SQL->GetError() );

Notice that this will check to see if AddToDB returns false. If it does, it kills the execution of the script and displays the error that can be gotten from the GetError method.

But you didn't think we'd stop there, did you? No, no my blue friend, there is always an update to be done.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: