Hook creation

adam123

New member
Is there anywhere to test if the code for a hook has errors?
I'm trying to create a hook which will enter the same value in a field in one table based on the field in another table being updated.
I've created a hook in Custom_functions.php - $hooks['check_stock']['insert']['after'] = 'location_update';
I can see the hook is showing in the admin area of dadabik.
The code that should run is below which should update the 'location' field in stock_list based on the check_stock location being updated. I've manually run the SQL query and that works fine but I can't get the hook to automatically run.

function location_update($check_stock_id)
{
global $conn;

// Retrieve stock_id and stock_fk from check_stock
$sql_select = "SELECT stock_id, stock_fk FROM check_stock WHERE check_stock_id = :check_stock_id";
$res_select = prepare_db($conn, $sql_select);
bind_param_db($res_select, ':check_stock_id', $check_stock_id);
$res_ex_select = execute_prepare_db($res_select, 1);

if ($row = fetch_result_db($res_ex_select)) {
$stock_id = $row['stock_id'];
$stock_fk = $row['stock_fk'];

// Prepare the SQL statement to update stock_list
$sql_update = "UPDATE stock_list
SET location = (
SELECT location
FROM check_stock
WHERE stock_fk = :stock_fk
AND checkdate = (
SELECT MAX(checkdate)
FROM check_stock
WHERE stock_fk = :stock_fk
)
)
WHERE stock_id = :stock_id";

// Prepare the statement
$res_prepare = prepare_db($conn, $sql_update);

// Bind parameters
bind_param_db($res_prepare, ':stock_id', $stock_id);
bind_param_db($res_prepare, ':stock_fk', $stock_fk);

// Execute the prepared statement
execute_prepare_db($res_prepare, 0);
}
}

Any advice on how to test the hook code or what needs to be changed to get this to run?
 
Top