4

I have been reading Form API Quickstart Guide. I understand how it all works, but I don't understand how to save the data entered into the forms into my own database Drupal style. Or am I supposed to use good old standard PHP to connect to the database to insert the data as required?

Is there a similar page to the form API quickstart page showing how to integrate a module with a database?

4

Connecting to the database and writing your own SQL, outside of the Drupal framework, while possible will put you in the dark ages. Drupal handles a lot of the heavy lifting and abstraction for you. If you are used to writing your own PHP to do DB queries it can be tempting to go with what you know. but in the cases where you need to use custom queries use the Drupal DB API like  Berdir suggested.

However often there are ways to use forms, without having to write your own database methods. Depending on what you want to do with the data there are several options.

Webform may be what you are after. Which will allow you to create custom forms with simple data management at the back end. Again fairly simple to implement but in this case you won't have the overhead of new nodes and the data is easily exportable.

In both of these cases you can use hook_form_alter to customize the form if you wish.

  • 1
    There are many reasons to write custom SQL and it doesn't put you in the dark ages, not everything can or should be done with Webform or CCK. – Berdir Apr 21 '11 at 10:21
  • I didn't mean to say that you shouldn't write your own SQL (will edit), but not using drupals Database API wherever possible is not generally a bad idea. – Jeremy French Apr 21 '11 at 10:27
  • Sounds better now :) – Berdir Apr 21 '11 at 11:06
3

Yes, there is, see Database abstraction layer.

In short, Drupal 6 provides a simple database abstraction (much better in D7), but apart from that you have to use standard SQL, with a few specialities like wrapping table names in curly braces.

You could also check out drupal_write_record(), which allows to save a single record in a table.

1

If you do need to manually update a table, the Form Example and Database Example in the Examples Project will get you going.

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.