0

This is the PHP part of an ajax request. The user enters two numbers, via two fields. I can add these two numbers together & display the total in a field, this works OK.

However, I would also like to add a number that has been retrieved from a custom table to this. So it's two user entered numbers + number from custom table. I get the following error:

br /><b>Fatal error</b>: Unsupported operand types in <b>/home/greentes/public_html/wp-content/plugins/jquery_validate_min1/activate2.php</b> on line <b>118</b><br />

Line 118 is
$total = $numwelds + $numconwelds + $mpcountrys ;

My code:

 function myajax_inputtitleSubmit_func() {

 global $wpdb;
$mp_country_table_info = $wpdb->prefix . 'countries '; //Good practice
$mpcountrys = $wpdb->get_results( "SELECT labour_cost FROM $mp_country_table_info WHERE id='2' ");


        $nonce = $_POST['nextNonce'];   
    if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) )
        die ( 'Busted!');

    $numwelds = isset($_POST['numberofwelds']) ? $_POST['numberofwelds'] : '';
$numconwelds = isset($_POST['numberofconwelds']) ? $_POST['numberofconwelds'] : '';

if (is_numeric($numwelds) && is_numeric($numconwelds))
{
    $total = $numwelds + $numconwelds + $mpcountrys ;
    $response = json_encode($total);
    header("Content-Type: application/json");  
    echo $response;
    exit;
} 
}

What is wrong with this please?

2

$mpcountrys is going to be an array or an object. In your case, an object, which is the default.

One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information.

https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

It looks to me like you probably want get_var() instead.

And I would highly recommend a bit of sanitaziation/validation of your user supplied values before trying to use them.

  • Thanks that makes sense. I've used is_numeric on the user supplied values, what else could I do for sanitation/validation please? – mattnewbie Mar 25 '14 at 17:58
  • Sorry, I didn't see the is_numeric() but I'd recommend you take a look at filters nonetheless. – s_ha_dum Mar 25 '14 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.