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
developerWorks - FREE Tools! |
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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! |