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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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!


    IBM DB2 Deep Compression ROI Tool

    The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times.
    FREE! Go There Now!


    NEW! Best Practices in Integrated Requirements Management

    Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right."
    FREE! Go There Now!


    NEW! Download DB2 9.5 for Linux, Unix, and Windows

    Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML.
    FREE! Go There Now!


    NEW! Download IBM WebSphere Portal V6.1 beta code

    Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization.
    FREE! Go There Now!


    NEW! Download the free Web Application Security eKit

    Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Developer for System i V7.1

    Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z

    IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Test terminal-based applications with Rational Functional Tester

    Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily.
    FREE! Go There Now!


    NEW! Webcast: Quickly provide customized, integrated user interfaces with Lotus Notes 8

    IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community.
    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-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek