Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Riya28 on
Hi all,
I'm new to drupal.
I have a button in display page of a node.
If i click the button in the page,it should fetch the currently logged in user id and the node id of the node where button belongs to and insert those id's into the custom table i created.Please give suggestions. Thanks in advance.
Comments
Insert node id and user id in to the custom table in D7
Hi,
For that you need to create custom module and put below piece of code in to the module class method and call that method in button click.
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
if ($nid) {
$nodeId = node_load($nid);
global $user;
$user_id = $user->uid;
$nid = db_insert('your_table_name')
->fields(array(
'node_id' => $nodeId,
'user_id' => $user_id,
))
->execute();
}
}
Please let me know if you have any doubt.
Thanks,
Dibyajyoti