/ Published in: PHP
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:-)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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 ) { { $vals = $_POST; } foreach( $vals as $k => $v ) { { } } $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; ?>
URL: http://www.itsgotto.be/cv.php