iMed
iMed

Reputation: 113

Wordpress update query does not work , returns false

I want to update my custom table in wordpress database, i am using a very basic query but it does't work for me !

In fact the queries are inside a function ( ajax callback ), The first (SELECT ) works fine and returns data .

But the 2nd does not ! And returns always false .

N.B : I put the 1st query of select to prove that ajax sends the data correctly, and the URL is correct

Please help me to detect where is the problem ?

<?php
function activate_calbk()
{
   //1 this works very well :) 
   if (isset($_POST['req']) and ($_POST['req']=='verif'))
        {
            global $wpdb;
            $quer = $wpdb->get_results( "SELECT * FROM my_table" );
            $arr = $quer[0]->etat;
            echo $arr;
            die();
        }


    // 2  not working :(  ! 
    else if ( isset($_POST['req']) and ($_POST['req']=='activate'))
        {
           global $wpdb;
           $ereminders= $wpdb->query("UPDATE $wpdb->my_table SET etat = 'off' WHERE id = '1'");

            if ($ereminders) 
            {
                echo 'updated';
                die();
            }



        }
    wp_die(); // required. to end AJAX request.
}

Upvotes: 0

Views: 782

Answers (1)

urban_biscuit
urban_biscuit

Reputation: 86

Have you tried using the wpdb update?

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

$wpdb->update( 
    'my_table', 
    array( 
        'etat' => 'off',    // string
    ), 
    array( 'id' => 1 ), 
    array( 
        '%s',   // string
    )
);

Upvotes: 1

Related Questions