1

i'm trying to insert data to a different data base that i created. I've read a lot in the forum, the support documents and other pages but i haven been able to figure it out the problem, can someone give me a hand? point me to a right direction?

first i modified the settings.php like this:

<?php
/** ORIGINAL DATA BASE CONNECTION
$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'dbname',
      'username' => 'dbuser',
      'password' => 'dbpsw',
      'host' => 'dbhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);
*/

/** DOUBLE DATA BASE CONNECTION  */
$databases = array();
$databases['default']['default'] = array(
   'database' => 'dbname',
      'username' => 'dbuser',
      'password' => 'dbpsw',
      'host' => 'dbhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
);
$databases['civilproconstru']['default'] = array(
   'database' => 'name',
      'username' => 'username',
      'password' => 'password',
      'host' => 'datahost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
);
?>

And then in the module i'm creating i'm using this code: (i'm pasting the full module code, the insert is at the end, just in case the error is in the back)

<?php
/** START CONFIG MODULE
 * Implements hook_permission().
 */
function project_creator_permission() {
  return array(
    'access project_creator content' => array(
      'title' => t('Access content for the project_creator module'),
    )
  );
}

 /**
 * Agrega a lista de estructura hook_block_info().
 */
function project_creator_block_info() {
  $blocks['project_creator'] = array(
    // The name that will appear in the block list.
    'info' => t('Project Creator'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

/**
 * Agrega menu en toolbar para "role" contratante hook_menu().
 */
function project_creator_menu() {
  $items['admin/crear_proyecto'] = array(
    'title' => 'Crear Proyecto',
    'description' => 'Crear Proyecto',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('project_creator_insert'),
    'access callback' => 'project_creator_access',
  );

  return $items;
}


function project_creator_access() {
 global $user;
 if (in_array('contratante', array_values($user->roles))){
     return TRUE;
 }
}
 /**
 * END CONFIG MODULE.
 */

/** START PROJECT CREATOR FORM */

function project_creator_insert($form, &$form_state){

    $form['p_name'] = array(
    '#type' => 'textfield',
    '#title' => 'p_name',
    '#size' => 15
    );

    $form['save'] = array(
    '#type' => 'submit',
    '#value' => 'save',
    '#submit' => array('project_creator_insert')
    );
    return $form;

db_set_active('civilproconstru'); /* OPEN DATA BASE*/
db_insert('project_info')
->fields(array(
'p_name' => $form_state['values']['p_name'],
))->execute();
drupal_set_message("succesfully saved");
db_set_active(); /*CLOSE DATA BASE*/

}
?>

At the end, it shows me the form but when i click save button it seems to be inserting the data but when i check the database it still empty. It doesnt give me any kind of errors on execution. Thank u for the help...

2
  • 1
    The settings.php and db_set_active code look ok. My guess is that the other database isn't accessible from your host, e.g firewalled on a remote host. Make sure the other db can be connected to from your Drupal web server, e.g via the mysql command line. Jan 10, 2016 at 8:12
  • The database is set at the same server of drupal database... it shouldnt be a problem... Mar 1, 2016 at 20:29

3 Answers 3

2

You don't have any submit callback. You should do any process with the submitted data in a submit callback like that:

function project_creator_insert_submit($form, &$form_state) {
  db_set_active('civilproconstru'); /* OPEN DATA BASE*/
  db_insert('project_info')
    ->fields(array(
      'p_name' => $form_state['values']['p_name'],
    ))->execute();
  drupal_set_message("succesfully saved");
  db_set_active(); /*CLOSE DATA BASE*/
}
3
  • where is the callback submit on your suggested code? dont see it.. Mar 1, 2016 at 20:31
  • The function project_creator_insert_submit() is the submit callback of your form function project_creator_insert(). Take a look at the documentation about Form generation.
    – Elin Y.
    Mar 1, 2016 at 21:13
  • new update... with new error.. Mar 2, 2016 at 2:21
1

Ok! i finally got it! this is the code that worked for me:

function project_creator_permission() { return array( 'access project_creator content' => array( 'title' => t('Access content for the project_creator module'), ) ); } function project_creator_block_info() { $blocks['project_creator'] = array( // The name that will appear in the block list. 'info' => t('Project Creator'), // Default setting. 'cache' => DRUPAL_CACHE_PER_ROLE, ); return $blocks; } function project_creator_menu() { $items['admin/crear_proyecto'] = array( 'title' => 'Crear Proyecto', 'description' => 'Crear Proyecto', 'page callback' => 'drupal_get_form', 'page arguments' => array('project_creator_form'), 'access callback' => 'project_creator_access', ); return $items; } function project_creator_access() { global $user; if (in_array('contratante', array_values($user->roles))){ return TRUE; } } function project_creator_insert($form, &$form_state) { $form['p_name'] = array( '#type' => 'textfield', '#title' => t('p_name'), '#required' => TRUE, '#description' => "Introduzca el nombre del proyecto.", ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', ); return $form; } function project_creator_insert_submit($form, &$form_state) { /OPEN DATA BASE/ db_set_active('secondDataBase'); db_insert('test') ->fields(array( 'p_name' => $form_state['values']['p_name'], ))->execute(); drupal_set_message(t('The form has been submitted.')); db_set_active(); /CLOSE DATA BASE/ } ?>

-2

In the code you are giving submit callback but not writing insert code inside that function. You should write insert code in a function like this:

function project_creator_insert($form, &$form_state) {
      db_set_active('civilproconstru');
      db_insert('project_info')
         ->fields(array(
         'p_name' => $form_state['values']['p_name'],
          ))->execute();

      drupal_set_message("succesfully saved");`
      db_set_active();`
}
2
  • 1
    Your syntax is not correct for the function structure.
    – kenorb
    Jan 11, 2016 at 10:20
  • 1
    Plus it's just a copy of a already given correct answer.
    – leymannx
    Mar 16, 2016 at 9:12

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.