It's super easy using php and SQL but how do you change the default database drupal 6 saves posted data in the forms? To an external database?

  • I feel your question is so generic. Can you explain more!! which forms are you referring to?
    – Aboodred1
    Commented Jun 24, 2013 at 21:47
  • Welcome to Drupal Answers. Ate you talking about the Webforms module or just forms in general?
    – mpdonadio
    Commented Jun 24, 2013 at 23:21
  • Hi sorry. function semicontacts_form_submit($form_id, &$form_data){ db_query("INSERT INTO drupalsemicontacts (firstname, lastname, phonenumber) VALUES
    – Tom
    Commented Jun 25, 2013 at 8:12
  • Hi sorry. This is an example of the query that submits the data to the DB. It automatically knows the default database drupal is installed into and submits it to that DB + (table name), and i can only specify the table name here? function names_form_submit($form_id, &$form_data){ db_query("INSERT INTO drupalnamescontacts (firstname, lastname, phonenumber) VALUES
    – Tom
    Commented Jun 25, 2013 at 8:18
  • I did try following [hello world 2] (semicolon.co.za/mysql_tutorials/…) but again it assumes you want to save to the local drupal database. @MPD
    – Tom
    Commented Jun 25, 2013 at 8:21

1 Answer 1


I would try reading through How to connect to multiple databases within Drupal (notably, the Drupal 6 section towards the end.

Basically, in your settings.php file, convert the $db_url to an array, and add the secondary database:


$db_url = 'mysql://drupal:drupal@localhost/drupal';


$db_url['default'] = 'mysql://drupal:drupal@localhost/drupal';
$db_url['db2'] = 'mysql://user:pass@localhost/db2';

Then create a submit handler that either compliments or overrides an existing form submit handler and use db_set_active to connect to the secondary database, save the data, and then switch back.


function MYMODULE_MY_EXTRA_SUBMIT_HANDLER($form, &$form_state){
  // Switch to db2  

  // Do all your form processing and database transactions

  // Connect back to default Drupal database
  // *Note* If your primary db is keyed with "default", then you don't need to
  // pass an argument to db_set_active().
  • Thanks for the great reply, this was what I was looking for however I didn't realise about the little snag of "This only works with two databases of the same type. For example the following code will not work." I actually have a postgresql DB that I am wanting to input data into. And that is obviously different to the drupal sql. I could reinstall drupal with postgresql as the DB. However this is not possible as the site is fixed and the client will not allow changes to this. Looks like I will have t use php/pgsql/html forms to submit the data.
    – Tom
    Commented Jun 25, 2013 at 13:30
  • Is there any other method other than using my own html/php forms t submit to my postgresql db from within drupal? do any of the other form mobules support submitting to an external DB of different datatype? @Chapabu
    – Tom
    Commented Jun 25, 2013 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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