Jump to content

add data to database hook when new client is created


twister

Recommended Posts

hi guys, 

in whmcs database i created a table

otherdata

i want to create a hook that will insert client id to this table when a new client is created. 

this is what i have so far 

 

<?php

add_hook('ClientAdd', 1, function($vars) {
    // Perform hook code here...


$userid = $vars['userid'];

$mysqli = new mysqli("localhost", "user", "password", "databasename");

$query= "INSERT INTO otherdata (id) VALUES ('$userid');";


if(mysqli_query($mysqli, $query)){
  //  echo "Records inserted successfully.";
} else{
   // echo "ERROR: Could not able to execute $sql. " . mysqli_error($mysqli);
}

// Close connection
mysqli_close($mysqli);




});

?>

please help understand what am i doing wrong. 

Link to comment
Share on other sites

3 hours ago, twister said:

please help understand what am i doing wrong. 

which version of WHMCS are you using ??

if it's v8, then 'userid' won't exist - it would either be 'client_id' (if you want the client value), or 'user_id' (if you want the user value),

Link to comment
Share on other sites

3 hours ago, twister said:

it's v8.1.  is the rest of the hook correct ?

the hook below would insert the clientid value into the other table when anew client is created..

<?php

use WHMCS\Database\Capsule;

function client_add_otherdata_hook($vars) {
	$clientid = $vars['client_id'];
	$otherdata = Capsule::table('otherdata')->insert(['clientid' => $clientid]);
}
add_hook("ClientAdd", 1, "client_add_otherdata_hook");

the above hook assumes that the otherdata table has a field called 'clientid' - if yours is called userid, then you can change 'clientid' to 'userid' (or whatever.

also, i'm not checking whether clientid contains a value before updating the database table... it might be wise to check first before insertion.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated