MySQL
  Home arrow MySQL arrow Page 6 - 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? 
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 / 89
    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 - UpdateDB in Action
    ( Page 6 of 7 )

    This will be similar to how we used AddToDB earlier, only this time, we want to query out all the people in the database and update the one we choose. First, let's output those names to the screen.


    <?php
    // Instantiate the class
    $SQL = new MyDatabase;
    // Connect to the database
    $SQL->Connect();
     
    ? >
    <html>
    <head>
    <title>Using UpdateDB</title>
    <meta http-equiv="Content-Type" 
    content
    ="text/html; charset=iso-8859-1">
    </head>
     
    <
    body>
    <table width="500" border="0" 
    cellspacing
    ="0" cellpadding="3">
     
    <tr bgcolor="#CCCCCC">
      
    <td width="121">Name</td>
      
    <td width="30">Age</td>
      
    <td width="79">Gender</td>
      
    <td width="246">Phone</td>
     
    </tr>
    <?php
     
    $results 
    mysql_query("SELECT 
    id,name,age,gender,phone FROM my_tbl 
    WHERE (1) ORDER BY name"
    ) or 
    die
    ("MYSQL ERROR: ".mysql_error());
     
    if(
    $row=mysql_fetch_assoc($results))
    {
     do
     
    {
      
    ? >
       
    <tr>
       
    <td><a href="<?php echo 
       $_SERVER['PHP_SELF']; 

    ?id=<?php 
       echo $row["id"]; 

    "
    ><?php echo 
       $row
    ["name"]; 

    </a></td>
       <td><?php echo $row["age"]; 

    </td>
       <td><?php echo $row["gender"]; 

    </td>
       <td><?php echo $row["phone"]; 

    </td>
       </tr>
      <?php
     
    }
     
    while ($row=mysql_fetch_assoc($results));
    }
    else
    {
     
    ? >
      
    <tr>
      
    <td colspan="4">No data</td>
      
    </tr>
     
    <?php
    }
    ? >

    </table><br>

    I trust that the above code needs no in-depth explanation. It's simply connecting to the database using the MyDatabase class and pulling an associative array while looping through the results. Now let's alter our HTML form from above to be used for updating and not inserting.


    <?php
    if($_GET['id'])
    {
     $results 
    mysql_query("SELECT 
     name,age,gender,phone FROM my_tbl 
     WHERE id="
    .$_GET['id']) or die("MYSQL
     ERROR: "
    .mysql_error());
     $row
    =mysql_fetch_assoc($results);
     


     <form action="<?php echo 
     $_SERVER
    ['PHP_SELF']; 


     method="post" name="myForm">
      <p>Name: <input name="name" 
      type="text" value="<?php echo
      $row
    ["name"]; 

    "></p>
      <p>Age: <input name="age" type="text"
      value="<?php echo $row["age"]; 

    "></p>
      <p>Gender:<br>
         <input name="gender"
      type="radio" value="MALE"<?php echo 
      
    ($row["gender"] == 'MALE') ? 
      
    ' checked' ''

    > Male<br>
         <input type="radio" 
      name="gender" value="FEMALE"<?php echo 
      
    ($row["gender"] == 'FEMALE') ? 
      
    ' checked' ''

    > Female</p>
      <p>Phone Number: <input name="phone" 
      type="text" value="<?php echo 
      $row
    ["phone"]; 

    "></p>
      <p><input name="action" type="submit"
      value="UpdateDB">
      <input name="id" type="hidden" 
      value="<?php echo $_GET["id"]; 

    "></p>
    </form>
    <?php
    }
    $SQL->Disconnect();
    ? >

    </body>
    </html>

    The only real difference here is the addition of:

    <input name="id" type="hidden"
    value="<?php echo $_GET["id"]; ?>">

    This is just a hidden form field to pass the id. You will also notice we are echoing out the data in the form fields like so:

    value="<?php echo $row["age"]; ?>"

    Alright, now we have got to make this beast actually update the data in the database table.



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

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT