Hello,

I want to use db_insert inside a custom created php file.
With some searching i found that i need to initialize soms files from the include directory.

While trying this, nothing seems to work.
my code from my custom php file.

the path to my boostrap.inc, database.inc and database.mysql.inc are correct.


// include needed files
	include('../../../../../includes/bootstrap.inc');
  	include('../../../../../includes/database.inc');
   	include('../../../../../includes/database.mysql.inc');

// Launch drupal start: configuration and database bootstrap
   conf_init();
   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
   drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

// Now you can use drupal database with drupal's dbal:
// Unlock user admin if blocked
   db_query("UPDATE {users} set status = 1 where uid = 1");
  

$nid = db_insert('table')
	->fields(array(
    	'numbers' => ('23456')
  		))
  	->execute();
	drupal_set_message(t('Content was added'));  

Hope someone can help me.

Comments

nitin.k’s picture



// include needed files
	include('../../../../../includes/bootstrap.inc');
  	include('../../../../../includes/database.inc'); // Path is wrong..
   	include('../../../../../includes/database.mysql.inc'); // Path is wrong. Drupal does not contain database.mysql.inc file.
s
// Warning: include(../../../../includes/database.mysql.inc): failed to open stream: No such file or directory


// Launch drupal start: configuration and database bootstrap
   conf_init(); // will give undefined fatal error.

 // Fatal error: Call to undefined function conf_init()

   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);   // will give warnings after removing fatal error and include functions warnings.
   drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);   // will give warnings after removing fatal error and include functions warnings.

// Now you can use drupal database with drupal's dbal:
// Unlock user admin if blocked
   db_query("UPDATE {users} set status = 1 where uid = 1");
  

$nid = db_insert('table')
	->fields(array(
    	'numbers' => ('23456')
  		))
  	->execute();
	drupal_set_message(t('Content was added'));  

The above code has lots of problem. Below is 100% working solution..

Create a file like somename.php in Drupal Root folder...and run below script.



define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Now you can use drupal database with drupal's dbal:
// Unlock user admin if blocked
db_query("UPDATE {users} set status = 1 where uid = 1");

$nid = db_insert('table')
->fields(array(
  'numbers' => ('23456')
  ))
->execute();
drupal_set_message(t('Content was added'));



Cheers !!

Gregory_v87’s picture

awsome! Thx!! :-)