/ Published in: PHP
                    
                                        
Takes your table name and key=>value array of values and returns the text for inserting into a MySQL database. Automatically surrounds strings with single quotes.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
function get_insert_query($table, $array) {
$insert_text = "INSERT INTO " . $table;
foreach ($array as $k=>$v) {
$keys[] = $k;
$values[] = $v;
}
$key_string = "(";
foreach ($keys as $key) {
$key_string = $key_string . $key . ", ";
}
$insert_text = $insert_text . " " . $key_string . ")";;
$insert_text = $insert_text . " VALUES ";
$value_string = "(";
foreach ($values as $value) {
$value_string = $value_string . "'" . $value . "', ";
}
else {
$value_string = $value_string . $value . ", ";
}
}
$insert_text = $insert_text . $value_string . ")";
return $insert_text;
}
echo get_insert_query("users", $data);
## Echos: INSERT INTO users (id, name, address) VALUES (23, 'David Lemcoe', '123 Green St.')
URL: http://blog.lemcoe.com/?p=61
Comments
 Subscribe to comments
                    Subscribe to comments
                
                