How do I pass field variables for a database insert helper function I am trying to set up a database insert helper function for a group of modules for a program I m using to learn Drupal.

I can't figure out how to use my returned array in my db_insert query, can anyone assist please.

I assumed I could use a for loop as below but that's not working for me, I really appreciate any help you can offer

function my_db_insert($table, $fields) {

// Build up a string based on my returned array via a for loop   
$insert_fields="";
foreach ($fields as $key=&gt;$value) {
    $insert_fields = $insert_fields . $key . "=&gt;" . $value . ",";
      //drupal_set_message($key . "=&gt;" . $value . ",");
   }

  // Insert into the database.
$id = db_insert($table)
-&gt;fields(array(
  //This is where I need put the result of my array so it will add to the database 
  $insert_fields
))
-&gt;execute();

return $id;

drupal_set_message(t("Record Added!"));


}
This was answered on the drupal.org forum by @Jaypan:

$id = db_insert($table)
  -&gt;fields($fields)
  -&gt;execute();