The reason I used float was because %d and %u were giving me problems with phone numbers =\
Also I type cast and verify correct data in the processing script before calling the function. (IE bool fields are 1 or 0, strings don't exceed the length, etc). And changed the $db to be passed as a parameter. However this function is for a site that's already live, and completely procedural, instead of trying to rewrite the whole site I'm just trying to clean up code (there were 27 different insert functions!) New function:
function dbInsert($db,$table,$info) {
$query = "INSERT INTO `$table` SET ";
foreach( $info as $k => $v) {
if( is_numeric($v) ) {
$query .= sprintf("`%s`=%f,",$k,$v);
} elseif ( $k == 'date_added' ) {
$query .= sprintf("`%s`=%s,",$k,$db->real_escape_string($v));
} else {
$query .= sprintf("`%s`='%s',",$k,$db->real_escape_string($v));
}
}
$query = substr($query,0,-1);
return $db->query($query);
}
How would you handle the number thing? If they are too large they get changed. I know this is because of the way integers and unsigned integers are represented in memory (2147483647 and 4294967294 as maximums)