Michael D
Michael D

Reputation: 621

Where is the best place to put a DB query in drupal?

I have a few forms who's data needs to be written to the main database in their own tables. Just simple name-email collection forms, that are part of regular pages in drupal. I suspect the right way to do this would be to write a module that would have the query in a function, but this is just a guess. I've never written a module before, and don't know how the form would be processed that way.

It's also a guess that to perform the db query within a node would be insecure?

The only other idea I have would be to use a php file on the server to do the form processing, with the db query written there, but that seems very non-drupal, as far as I can tell.

How to??

Upvotes: 1

Views: 447

Answers (3)

jpamental
jpamental

Reputation:

I'd second the Webform module. Create a really simple webform with name and email (results can be emailed or stored automatically in the DB, and can be exported easily in several formats). Then check out the Webform Block module to put that webform in a block and place wherever you need it on your site. No custom coding required!

Here is an excerpt from the Webform Block project page:

Allows embedding a webform node into a block which can be positioned in any block region (theme space permitting).

Handles redirecting back to the current page on validation errors.

A good use for this is to add a site wide contact form inline on your pages, such as in the footer or sidebar.

Multiple webforms can appear on the same page. The contents of the block is simply a node, so theme work can be done in the node-webform.tpl.php file as usual.

Upvotes: 0

Eli
Eli

Reputation: 5610

Have you checked what's already out there? Webform may be able to solve your needs without any custom code.

Also, I highly recommend the book Pro Drupal Development if you want to learn more about Drupal and basic module development.

Upvotes: 0

jcuenod
jcuenod

Reputation: 58395

http://drupal.org/node/508 is Drupal's info on module development

You'll be wanting to use the function "db_query()"

You can use Drupal's built in methods to build forms though that will save the data for you. Have a look at the module developers guide though and you should be able to figure it out. Module development sounds intimidating but it's not as hectic as it seems once you actually know what's going on with Drupal (hence, read the guide).

Upvotes: 2

Related Questions