• Resolved tragicdog

    (@tragicdog)


    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 = "https://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]));
    }
Viewing 1 replies (of 1 total)
  • Thread Starter tragicdog

    (@tragicdog)

    had a friend of mine look at it. he noticed the right square bracket. removed it and the code works.

    • This reply was modified 1 year, 2 months ago by tragicdog.
Viewing 1 replies (of 1 total)
  • The topic ‘using wpcode to insert into a custom table in WordPress DB’ is closed to new replies.