While what you have above works it's very cluttered with un-needed code, please see the below code for how you can do it with join(), array_keys() and array_values().
function mysql_insert($data) { if (is_array($data)) { $sql = "INSERT INTO " . $data['table'] . " (" . join(', ', array_keys($data['values'])) . ") VALUES ('" . join("', '", array_values($data['values'])) . "')";
if (!mysql_query($sql)) { die('MySQL Error: ' . mysql_error()); } } else { die('Invalid argument given, must be an array!'); } }
This is a pretty standard feature of most frameworks. I'm currently in the process of doing the same, although with prepared statements. Learning a lot of new things about dynamic prepared statements as well!
As @SgtLegend says, you can do things much quicker with array_keys() and array_values()
This would be the use:
Is this bad practice? or is there a better way I could/should be doing this?
join(),array_keys()andarray_values().As @SgtLegend says, you can do things much quicker with array_keys() and array_values()