1

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?

5
  • I feel your question is so generic. Can you explain more!! which forms are you referring to?
    – Aboodred1
    Jun 24 '13 at 21:47
  • Welcome to Drupal Answers. Ate you talking about the Webforms module or just forms in general?
    – mpdonadio
    Jun 24 '13 at 23:21
  • Hi sorry. function semicontacts_form_submit($form_id, &$form_data){ db_query("INSERT INTO drupalsemicontacts (firstname, lastname, phonenumber) VALUES
    – Tom
    Jun 25 '13 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
    Jun 25 '13 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
    Jun 25 '13 at 8:21
6

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:

e.g.

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

becomes

$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.

e.g.

function MYMODULE_MY_EXTRA_SUBMIT_HANDLER($form, &$form_state){
  // Switch to db2  
  db_set_active('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().
  db_set_active('default');
}
2
  • 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
    Jun 25 '13 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
    Jun 25 '13 at 15:23

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.