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!


    NEW! Accelerating Software Innovation on i on Power Systems

    Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time.
    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! IBM Rational Systems Development e-Kit

    As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br />
    FREE! Go There Now!


    NEW! Info 2.0: Harnessing the power of Web 2.0 and Enterprise Mashups

    Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started.
    FREE! Go There Now!


    NEW! Rational Modeling Extension for Microsoft.Net

    Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms.
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Successful Change and Release Management for .NET

    Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months.
    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! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    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-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek