Database Code
  Home arrow Database Code arrow Database query generator
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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? 
DATABASE CODE

Database query generator
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-01-18

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    These functions take input from a form and generate a string which can be used in a database query. If you do a lot of database work, this will make your life much easier.

    By : Ohwoww

    <?php

    #####################################################
    #
    # Form->Database Functions
    # Written By Gil Hildebrand Jr (root@moflava.net)
    # Use is granted under GNU Public License
    #
    #####################################################

    ####################
    # do_insert function
    ####################
    # Purpose: Produces 2 strings which can be used to make a database insert.
    #
    # How it works: In your form, name fields with "do_" as a prefix. For example,
    # if the field name in your db is "foobar", then name your form field
    # "do_foobar". Note that you can also make required fields, which will
    # halt the program before any database call if the required field has
    # no value. To use this, name your required field "do_required_foobar".
    #
    # In your program, call the function as follows:
    # list($fields,$values) = do_insert($HTTP_POST_VARS);
    #
    # The function will return an array which is broken into the $fields
    # and $values variables. To insert into your db, just do this:
    # mysql_query("Insert into table_name ($fields) VALUES ($values)");
    #
    # Usage: list($fields,$values) = do_insert($HTTP_POST_VARS);
    # if(!empty($values)) mysql_query("Insert into table_name ($fields) VALUES ($values)");
    ##################
    function do_insert($vars) {
    while(list($key,$value) = each($vars)) {
    if(preg_match("/do\_/i",$key)) {
    if(is_array($value)) {
    $x=0;
    while(list($key2,$value2)=each($value)) {
    $valinput .= $value2;
    if($x<count($value)-1) { $valinput .= ",";$x++; }
    }
    $columns[] = $key;
    $values[] = $valinput;
    $x=0;$valinput = "";
    }
    else if($value!="") {
    $columns[] = $key;
    $values[] = $value;
    }
    }
    if(preg_match("/requ\_/i",$key) && empty($value)) die("The $key field cannot be left empty. Please go back and fill in this field.");
    }

    $numcols = count($columns);
    $numvals = count($values);

    $columns = preg_replace("/do\_/i", "", $columns);
    $columns = preg_replace("/requ\_/i", "", $columns);

    for($i=0;$i<$numcols;$i++) {
    $columnstring .= $columns[$i];
    if($i<$numcols-1) $columnstring .= ",";
    }
    for($i=0;$i<$numvals;$i++) {
    $valuestring .= "'$values[$i]'";
    if($i<$numvals-1) $valuestring .= ",";
    }
    $return[0] = $columnstring;
    $return[1] = $valuestring;
    return $return;
    }

    ###################
    # do_update function
    ###################
    # Purpose: Produces a string which can be used to make a database update.
    #
    # How it works: In your form, name fields with "do_" as a prefix. For example,
    # if the field name in your db is "foobar", then name your form field
    # "do_foobar". Note that you can also make required fields, which will
    # halt the program before any database call if the required field has
    # no value. To use this, name your required field "do_required_foobar".
    #
    # In your program, call the function as follows:
    # list($fields,$values) = do_insert($HTTP_POST_VARS);
    #
    # The function will return a variable which can be used as the
    # string for your update query. Example:
    # mysql_query("Update table_name SET $updatestring WHERE foo='bar'");
    #
    # Usage: $updatestring = do_update($HTTP_POST_VARS);
    # if(!empty($updatestring)) mysql_query("Update table_name SET $updatestring WHERE foo='bar'");
    ################
    function do_update($vars) {
    while(list($key,$value) = each($vars)) {
    if(preg_match("/do\_/i",$key)) {
    if(is_array($value)) {
    $x=0;
    while(list($key2,$value2)=each($value)) {
    $valinput .= $value2;
    if($x<count($value)-1) { $valinput .= ",";$x++; }
    }
    $columns[] = $key;
    $values[] = $valinput;
    $x=0;$valinput = "";
    }
    else if($value!="") {
    $columns[] = $key;
    $values[] = $value;
    }
    }
    if(preg_match("/requ\_/i",$key) && empty($value)) die("The $key field cannot be left empty. Please go back and fill in this field.");
    }

    $numcols = count($columns);
    $numvals = count($values);

    $columns = preg_replace("/do\_/i", "", $columns);
    $columns = preg_replace("/requ\_/i", "", $columns);

    for($i=0;$i<$numcols;$i++) {

    $updatestring .= $columns[$i] . "='" . $values[$i] . "'";

    if($i<$numcols-1) $updatestring .= ", ";

    }
    return $updatestring;
    }

    ?>
    /*
    For database input:
    $updatestring = do_update($HTTP_POST_VARS);
    if(!empty($updatestring)) sql_query("Update table_name SET $updatestring WHERE foo='bar'");
    */
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! Evaluate Rational Host Access Transformation Services (HATS) Toolkit V7.1

    Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications.
    FREE! Go There Now!


    NEW! Hacking 101

    Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today.
    FREE! Go There Now!


    NEW! IBM Rational ClearCase Innovator's Series

    Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    DATABASE CODE ARTICLES

    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser
    - CSV to SQL convertor






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway