By krazykanuk on
Is it possible to do database switching in a validate or submit function? I ask because I try and switch in a validate function to check that the information entered in a form is correct, and it takes the site offline.
function mymyodule_validate(&$form, &$form_state) {
db_set_active('whatever');
db_query("SELECT * FROM {table} WHERE id = %d", 1);
db_set_active('default');
}
Comments
I can't think of any reason
I can't think of any reason why that wouldn't work. As long as you're not calling any functions that require access to the default database before you reset the active database it should work fine
OK because I have this at the
OK because I have this at the very top of my php file.
I have an administration settings form to determine the database name, database user, database password and the database hostname.
Now if I don't switch databases in the validate, it saves the information in the proper database as it should, but if I include a database switch it doesn't save the information and says site is off line. In the validate function after I validate that I am receiving characters when I should and numbers when I should, I check the information_schema to see if the supplied database exists. If I get an error back I could assume that 1 - database name is wrong 2 - the username is wrong 3 - password is wrong 4 - user doesn't have access to the information_schema database. Once I can do this I want to do the same for username and password.
This takes site offline but if I comment it out it works perfect without checking if database exists
_
might be obvious, but you don't mention it anywhere-- did you define and verify the db info in the settings.php file?
It not in the settings.php
It not in the settings.php file it done on the fly. But if I take the switch to schemadb out it passes the verify and submits the information and the drupal id, username, password gets inserted to the freeradius database once a new user registers. The on the fly is explained at http://drupal.org/node/18429 as well as using the settings.php, I altered it a bit for my use. The post I have above with the code shows how I define the switching.
I have found if I enter the 3
I have found if I enter the 3 databases into the sites settings.php file as:
that I can get it to work after I comment out the respective sections. I am also assuming that once finished and released who ever installs the module will use a consistant database driver and not mix and match them, and also they use the ['default'],['schemadb'] and ['freeradius'] as above (but obviously substituting there database name, database username and database password) if they don't it would break the module.
If I put it back to how I had it before and not try and validate if the database exists or if username and password are correct, then it will work and when a new user registers it will do as expected and put the username and password into the second database. The problem arises when someone with administrater permissions goes to the form and uses either an incorrect database name or an incorrect username/password, it will save to the drupal database the information and when a new user tries to register it will say site is offline because something related to the new database is incorrect.
The other person testing this as I work on it (as well as me) is finding that it is more convienient and user friendly to enter the information in with the form but is also more prone to errors. Is there a way I can use the on the fly method of switching databases as described here http://drupal.org/node/18429
and also validate the information in the form to make sure it is correct? Below I will include what works as long as the form has the correct database information.
In the settings.php the $db_url set to the following:
$db_url = 'mysqli://username:password@localhost/database';In the top of the freeradiu.module file just under the php tag:
The validate function that works as long as the database information is correct:
If I use the settings.php method adding the following to the bottom of the validate function works:
If I use the form method the above takes the site offline.