0

I have read a great deal and trying to learn how to get data into a table in the db. I have the below code to test entering just the first name of someone and I am getting an error. I have tried to follow the examples.module but I am missing something. I would appreciate a nudge in the right direction...

function ghost_form_form_submit($form, &$form_state) {
$entity_id = db_insert('field_data_field_first_name') // database table name
  ->fields(array(
    'field_first_name_value' => $form_state['values']['field_first_name_value'],
  )) 
  ->execute();

drupal_set_message(t('Your form entry has been added'));
}

Here is the complete form code... https://gist.github.com/anonymous/f6d8dc19a3eed0f56d59

The only error I get in watchdog is the $entity_id. However I am not sure what I am missing. Thanks

  • 1
    You shouldn't attempt to manipulate field tables directly as it can lead to inconsistencies, relevant caches aren't flushed, and so on. Always use the API (node_save(), field_attach_submit(), etc) – Clive Sep 9 '14 at 15:16
2

This message is probably because you missed some database fields in your insert query (we do not know the field type and what exactly you are trying to achieve). Other than that, it is very odd that you attempt to insert a record directly in a Drupal field's table as this not how it is meant to be used. As per Clive's comment, this can lead to data inconsistencies, cache issues etc.

So I would strongly recommend you to take a good look at Drupal examples on how to define a custom table using Schema API, or even better take a look at this guide on using the Entity API, which you allow you to properly utilize Drupal Fields with your own custom module. Third option is to use the API of an existing module such as nodes, users etc. so that you properly save the field's value.

|improve this answer|||||
  • Thank you. The hard part is not being a developer with a huge background in php. I will go through the Guide above. – timwhelan Sep 9 '14 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.