-1

I have a custom module and in this module I've a .php file and inside this file I need to insert the data into database. I am using below code:

  define('DRUPAL_ROOT', getcwd());
  header('Access-Control-Allow-Origin: *');
  include_once DRUPAL_ROOT . '../includes/bootstrap.inc';

  include_once DRUPAL_ROOT . '../includes/database.inc';
  include_once DRUPAL_ROOT . '../includes/database.mysql.inc';
  conf_init();
  drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
  drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  $userid = $_POST['user'];
  $module_name = $_POST['module'];
  $name = "_user_role_".$userid;
  $fe_id = db_insert('role')
    ->fields(array(
      'name' => $name,
    ))
    ->execute();

Its give below error: enter image description here

1

getcwd gets the current working directory, i.e. the one you're executing the script from.

If you need DRUPAL_ROOT to refer to the root of your Drupal installation, run the script from that directory, not the one that contains your module.

If you want to execute it from the module directory, fix the paths to the includes in your script.

0

Your code has issue in first line it-self:

define('DRUPAL_ROOT', getcwd());

getcwd() as per described on PHP.net

Gets the current working directory

Because of getcwd() your Drupal Root directory is set to your module directory and PHP isn't able to find bootstrap.inc and database.inc.

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.