1 dbtng_example.module | dbtng_example_entry_insert($entry) |
Save an entry in the database.
The underlying DBTNG function is db_insert().
In Drupal 6, this would have been:
db_query(
"INSERT INTO {dbtng_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
Related topics
File
- modules/
examples/ dbtng_example/ dbtng_example.module, line 92 - This is an example outlining how a module can make use of the DBTNG database API in Backdrop.
Code
function dbtng_example_entry_insert($entry) {
$return_value = NULL;
try {
$return_value = db_insert('dbtng_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;
}