using wpcode to insert into a custom table in WordPress DB

Hello, I am using WordPress 6.3.1, with WPCode Lite Version 2.1.2.

I have a snippet that if I uncomment the wpdb->insert line (2nd to last line in the code below) doesn’t appear to run.

Does anyone have any ideas? I checked the SQL db on the CLI and my WordPress user has GRANT ALL PRIVILEGES on ‘DB’.* so I don’t think it’s a permissions issue.

if(isset($_POST['origin']) && isset($_POST['destination']) && isset($_POST['tripDate']))  
{  
  // URL of Bing Maps REST Routes API;   
  $baseURL = "http://dev.virtualearth.net/REST/v1/Routes";  
  
  // Set key based on user input  
  $key = ""; //removed key for this post.   
  
  // construct parameter variables for Routes call  
  $wayPoint0 = str_ireplace(" ","%20",$_POST['origin']);  
  $wayPoint1 = str_ireplace(" ","%20",$_POST['destination']);  
  $optimize = "time";
  $routePathOutput = "Points";  
  $distanceUnit = "mi";  
  $travelMode = "Driving";  
  $tripDate = $_POST['tripDate'];
  $origin = $_POST['origin'];
  $destination = $_POST['destination'];
  
  // Construct final URL for call to Routes API  
  $routesURL = $baseURL."/".$travelMode."?wp.0=".$wayPoint0."&wp.1=".$wayPoint1."&optimize=".$optimize."&routePathOutput=".$routePathOutput."&distanceUnit=".$distanceUnit."&output=xml&key=".$key;  
  
  // Get output from API and convert to XML element using php_xml  
  $output = file_get_contents($routesURL);    
  $response = new SimpleXMLElement($output);  
  
  $travelDistance = $response->ResourceSets->ResourceSet->Resources->Route->TravelDistance;
  $travelDistance = floatval($travelDistance);
  
  $roundedDistance = round($travelDistance, 2);
  
  echo "On ". $tripDate . " you travelled " . $roundedDistance ." miles from " . $origin . " to " . $destination . ".";

  require_once $_SERVER['DOCUMENT_ROOT'] . '/wordpress/wp-config.php';
  global $wpdb;
  global $current_user; wp_get_current_user(); 
  $currentUser = $current_user->display_name;  
//$wpdb->insert('mt_trips', array('currentUser' => $currentUser, 'tripDate' => $tripDate, 'roundedDistance' => $roundedDistance, 'origin' => $origin, 'destination' => $destination]));
}

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer