<?php
//test data
$columns = array(
'username'=>'john',
'password'=>'hello',
'userlevel'=>1,
'date'=>'__d'
);
$table = 'users';
// replace keys and values with SQL delimeters
foreach($columns as $k=>$v) {
unset($columns[$k]);
if ($v != '__d' && !is_int($v))
$v = "'$v'";
if ($v == '__d')
$v = 'NOW()';
$columns["`$k`"] = $v;
}
// create the query
$sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
$table,
implode(",", array_keys($columns)),
implode(",", $columns)
);
echo $sql;
?>
Выход:
INSERT INTO users (`username`,`password`,`userlevel`,`date`) VALUES ('john','hello',1,NOW())