Sercan Demir
Sercan Demir

Reputation: 1

How to run some SQL commands on Drupal 7 User Rregistration

I need to run a SQL command right after a new user account has been created (I need to obtain new User ID also). (in Drupal 7)

How can I do this?

Upvotes: 0

Views: 48

Answers (2)

Muhammad Reda
Muhammad Reda

Reputation: 27023

Build a custom module with hook_user_insert implementation.

Here's a code sample that saves the new user's id into a table called test_table.

function [YOUR_MODULE]_user_insert(&$edit, $account, $category) {

    $newUserId = $account->uid;
    db_insert('test_table')
        ->fields(array(
            'user_id' => $newUserId,
        ))
        ->execute();
}

Upvotes: 0

oknate
oknate

Reputation: 1148

In a custom drupal module, use hook_user_insert.

within that function the $account variable with have the new user data and user id number.

To run custom sql query, you can use drupal's db_query function.

If you need to learn about custom drupal module development, take a look at the example modules.

Upvotes: 1

Related Questions