1 database_example.module database_example_entry_insert($entry)

Saves an entry in the database using db_insert().

In Drupal 6, this would have been:

  db_query(
    "INSERT INTO {database_example} (name, surname, age)
      VALUES ('%s', '%s', '%d')",
    $entry['name'],
    $entry['surname'],
    $entry['age']
  );

Exception handling is shown in this example. It could be simplified without the try/catch blocks, but since an insert will throw an exception and terminate your application if the exception is not handled, it is best to employ try/catch.

Parameters

array $entry: An array containing all the fields of the database record.

See also

db_insert()

Related topics

File

modules/examples/database_example/database_example.module, line 81
Hook implementations for the Database Example module.

Code

function database_example_entry_insert($entry) {
  $return_value = NULL;

  try {
    $return_value = db_insert('database_example')
      ->fields($entry)
      ->execute();
  }
  catch (Exception $e) {
    backdrop_set_message(t('db_insert() failed. Message = %message, query = %query', array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
  }

  return $return_value;
}