twister Posted March 10, 2021 Share Posted March 10, 2021 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. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted March 10, 2021 Share Posted March 10, 2021 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), 0 Quote Link to comment Share on other sites More sharing options...
twister Posted March 10, 2021 Author Share Posted March 10, 2021 it's v8.1. is the rest of the hook correct ? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted March 10, 2021 Share Posted March 10, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
twister Posted March 11, 2021 Author Share Posted March 11, 2021 thx mate, that did it. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.