MySQL
  Home arrow MySQL arrow Page 4 - Dynamically Insert and Update Values In a MySQL Database Using OOP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Dynamically Insert and Update Values In a MySQL Database Using OOP
By: Sam 'SammyK' Powers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 90
    2004-02-04


    Table of Contents:
  • Dynamically Insert and Update Values In a MySQL Database Using OOP
  • Essential Connection
  • Adam Up
  • If You POST It, It Will Go
  • Updateagenessly
  • UpdateDB in Action
  • You Wouldn't Have to Update It Had You Gotten It Right the First Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Dynamically Insert and Update Values In a MySQL Database Using OOP - If You POST It, It Will Go
    ( Page 4 of 7 )

    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',
      PRIMARY KEY  
    (`id`)
    TYPE=MyISAM AUTO_INCREMENT=;

    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>
      
    <p>Gender:<br>
         
    <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>
    </form>

    The rest is really simple.


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

    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.


    if(!$SQL->AddToDB('my_tbl'))
     
    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
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek