Revision: 15104
Updated Code
at October 11, 2011 19:14 by brownrl
Updated Code
<?php
// Function: Insert From Vals
// Take an associative array and build an insert statement
//
// $table -> the table you want to fill
// $prefix -> the prefix of the fields ( ie, auto_color -> 'auto_' )
// $vals -> the array to insert, default _POST
//
// Please note that this will work with normal database naming and not with
// special names with spaces and accents and odd stuff
//
//
function insertFromVals( $table , $prefix , $vals = null )
{
$fields = array();
$vallues = array();
if( is_null( $vals ) )
{
$vals = $_POST;
}
foreach( $vals as $k => $v )
{
if( preg_match( "/^".$prefix."/" , $k ) )
{
$fields[] = mysql_escape_string( $k );
$values[] = mysql_escape_string( $v );
}
}
$fields = join( "," , $fields );
$values = "'" . join( "', '" , $values ) ."'";
$q = "INSERT INTO ".$table." (".$fields.") VALUES (".$values.")";
return $q;
}
/// testing code here:
/// probably not what you want to copy
/// illustrative purposes only
$test['user_name'] = "Doe";
$test['user_fname'] = "John";
$test['user_birthday'] = "1977-12-16 00:00:00";
$test['user_favorite_color'] = "orange";
$test['user_attempted_injection'] = "a string with a \"'\" can be dangerous in a db statement";
$q = insertFromVals( "users" , "user_" , $test );
echo $q;
?>
Revision: 15103
Updated Code
at June 27, 2011 19:20 by brownrl
Updated Code
<?php
// Function: Insert From Vals
// Take an associative array and build an insert statement
//
// $table -> the table you want to fill
// $prefix -> the prefix of the fields ( ie, auto_color -> 'auto_' )
// $vals -> the array to insert, default _POST
//
// Please note that this will work with normal database naming and not with
// special names with spaces and accents and odd stuff
//
//
function insertFromVals( $table , $prefix , $vals = null )
{
$fields = array();
$vallues = array();
if( is_null( $vals ) )
{
$vals = $_POST;
}
foreach( $vals as $k => $v )
{
if( ereg( "^".$prefix , $k ) )
{
$fields[] = mysql_escape_string( $k );
$values[] = mysql_escape_string( $v );
}
}
$fields = join( "," , $fields );
$values = "'" . join( "', '" , $values ) ."'";
$q = "INSERT INTO ".$table." (".$fields.") VALUES (".$values.")";
return $q;
}
/// testing code here:
/// probably not what you want to copy
/// illustrative purposes only
$test['user_name'] = "Doe";
$test['user_fname'] = "John";
$test['user_birthday'] = "1977-12-16 00:00:00";
$test['user_favorite_color'] = "orange";
$test['user_attempted_injection'] = "a string with a \"'\" can be dangerous in a db statement";
$q = insertFromVals( "users" , "user_" , $test );
echo $q;
?>
Revision: 15102
Updated Code
at June 27, 2011 19:12 by brownrl
Updated Code
<?php
// Function: Insert From Vals
// Take an associative array and build an insert statement
//
// $table -> the table you want to fill
// $prefix -> the prefix of the fields ( ie, auto_color -> 'auto_' )
// $vals -> the array to insert, default _POST
//
// Please note that this will work with normal database naming and not with
// special names with spaces and accents and odd stuff
//
//
function insertFromVals( $table , $prefix , $vals = null )
{
$fields = array();
$vallues = array();
if( is_null( $vals ) )
{
$vals = $_POST;
}
foreach( $vals as $k => $v )
{
if( ereg( "^".$prefix , $k ) )
{
$fields[] = mysql_escape_string( $k );
$values[] = mysql_escape_string( $v );
}
}
$fields = join( "," , $fields );
$values = "'" . join( "', '" , $values ) ."'";
$q = "INSERT INTO ".$table." (".$fields.") VALUES (".$values.")";
return $q;
}
$test['user_name'] = "Doe";
$test['user_fname'] = "John";
$test['user_birthday'] = "1977-12-16 00:00:00";
$test['user_favorite_color'] = "orange";
$test['user_attempted_injection'] = "a string with a \"'\" can be dangerous in a db statement";
$q = insertFromVals( "users" , "user_" , $test );
echo $q;
?>
Revision: 15101
Updated Code
at June 25, 2009 06:20 by brownrl
Updated Code
// function InsertFromPost
// Take the post data and put it into a database
//
// $table -> the table you want to fill
// $prefix -> the prefix of the fields ( ie, auto_color -> 'auto_' )
//
function insertFromPost( $table , $prefix )
{
foreach( $_POST as $k => $v )
{
if( ereg( "^".$prefix , $k ) )
{
$fields .= mysql_escape_string( $k ) . ", ";
$values .= "'" . mysql_escape_string( $v ) . "', ";
}
}
$fields = ereg_replace( ", $" , "" , $fields );
$values = ereg_replace( ", $" , "" , $values );
$q = "INSERT INTO ".$table." (".$fields.") VALUES (".$values.")";
$qr = dbquery( $q );
return $qr;
}
Revision: 15100
Updated Code
at June 25, 2009 05:51 by brownrl
Updated Code
// function InsertFromPost
// Take the post data and put it into a database
//
// $table -> the table you want to fill
// $prefix -> the prefix of the fields ( ie, auto_color -> 'auto_' )
//
function insertFromPost( $table , $prefix )
{
foreach( $_POST as $k => $v )
{
if( ereg( "^".$prefix , $k ) )
{
$fields .= mysql_escape_string( $k ) . ", ";
$values .= "'" . mysql_escape_string( $v ) . "', ";
}
}
$fields = ereg_replace( ", $" , "" , $fields );
$values = ereg_replace( ", $" , "" , $values );
$q = "INSERT INTO ".$table." (".$fields.") VALUES (".$values.")";
$qr = dbquery( $q );
return true;
}
Revision: 15099
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 24, 2009 06:58 by brownrl
Initial Code
// function InsertFromPost
// Take the post data and put it into a database
//
// $table -> the table you want to fill
// $prefix -> the prefix of the fields ( ie, auto_color -> 'auto_' )
//
function insertFromPost( $table , $prefix )
{
foreach( $_POST as $k => $v )
{
if( ereg( "^".$prefix , $k ) )
{
$fields .= $k . ", ";
$values .= "'" . mysql_escape_string( $v ) . "', ";
}
}
$fields = ereg_replace( ", $" , "" , $fields );
$values = ereg_replace( ", $" , "" , $values );
$q = "INSERT INTO ".$table." (".$fields.") VALUES (".$values.")";
$qr = dbquery( $q );
return true;
}
Initial URL
http://www.itsgotto.be/cv.php
Initial Description
This is a long over due update to my db insert building function. Please not that the function is the important part the code below is just prove that it works... l:-)
Initial Title
PHP InsertFromVals
Initial Tags
form, database
Initial Language
PHP