query - Using $wpdb->update but confused on the WHERE in and SET

admin2025-06-03  2

I'm confused on using the where in the $wpdb->update :/ These last few lines of my code are MEANT to update a table with an IP address when software is activated. If they change IPs it records the 2nd IP address. It Activates NOW but doesn't record in table the IP's & throws a error log after and i know that i am misunderstanding the $wpdb->update. Before i was using MySQL code on separate script & DB and it worked but my error is just bad query. error says: Unknown column '0' in 'where clause' for query UPDATE ma1n_ipn_data_tbl SET 0 = '118.210.134.xxx' WHERE 0 = '1' CODE: HERE is the lines more clearly if below code pasted incorrectly:

I need some help if you could give an example of: update table set IP to $variable from getip.php where item_name = etc.. Thanks :P CODE:

                          //ALL WORKS ABOVE// 
        if ($row->ip_address_01 == 0){ 
         $wpdb->update($ipn_table, array( $row->ip_address_01 = $ipONactivate ),array(($row->payer_email = $serial) && ($row->item_name = $product)));  
    //  $sql=( "UPDATE $ipn_table SET ip_address_01 = $ipONactivate WHERE payer_email = $serial && item_name = $product" ); //BEFORE
     } else { 
        if ($row->ip_address_01 !== $ipONactivate && $row->ip_address_02 == 0){
        $wpdb->update($ipn_table, array( $row->ip_address_02 = $ipONactivate),array($row->payer_email = $serial && $row->item_name = $product));    
    //  $sql=( "UPDATE $ipn_table SET ip_address_02 = $ipONactivate WHERE payer_email = $serial && item_name = $product" ); //BEFORE
        } else {    
            echo false;
        }
       }                                                        //ERROR
                        //Unknown column '0' in 'where clause' for query UPDATE `ma1n_ipn_data_tbl` SET `0` = '118.210.134.xxx' WHERE `0` = '1'

I'm confused on using the where in the $wpdb->update :/ These last few lines of my code are MEANT to update a table with an IP address when software is activated. If they change IPs it records the 2nd IP address. It Activates NOW but doesn't record in table the IP's & throws a error log after and i know that i am misunderstanding the $wpdb->update. Before i was using MySQL code on separate script & DB and it worked but my error is just bad query. error says: Unknown column '0' in 'where clause' for query UPDATE ma1n_ipn_data_tbl SET 0 = '118.210.134.xxx' WHERE 0 = '1' CODE: HERE is the lines more clearly if below code pasted incorrectly: http://prntscr/mkiukd

I need some help if you could give an example of: update table set IP to $variable from getip.php where item_name = etc.. Thanks :P CODE:

                          //ALL WORKS ABOVE// 
        if ($row->ip_address_01 == 0){ 
         $wpdb->update($ipn_table, array( $row->ip_address_01 = $ipONactivate ),array(($row->payer_email = $serial) && ($row->item_name = $product)));  
    //  $sql=( "UPDATE $ipn_table SET ip_address_01 = $ipONactivate WHERE payer_email = $serial && item_name = $product" ); //BEFORE
     } else { 
        if ($row->ip_address_01 !== $ipONactivate && $row->ip_address_02 == 0){
        $wpdb->update($ipn_table, array( $row->ip_address_02 = $ipONactivate),array($row->payer_email = $serial && $row->item_name = $product));    
    //  $sql=( "UPDATE $ipn_table SET ip_address_02 = $ipONactivate WHERE payer_email = $serial && item_name = $product" ); //BEFORE
        } else {    
            echo false;
        }
       }                                                        //ERROR
                        //Unknown column '0' in 'where clause' for query UPDATE `ma1n_ipn_data_tbl` SET `0` = '118.210.134.xxx' WHERE `0` = '1'
Share Improve this question edited Feb 13, 2019 at 12:49 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 13, 2019 at 12:32 SchmutlySchmutly 391 gold badge2 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You need to look at the documentation for the function, which is available here.

The function takes the following arguments:

  • $table The table name.
  • $data A PHP array where the keys are the columns and the values are the the values to be inserted into those columns.
  • $where A PHP array where the key is the column name, and the value is the value to be checked.

And two more optional arguments that can be used to force the values of 2nd and 3rd arguments to be certain types. See the documentation for more on those.

What you've done is mixed up PHP and SQL syntax for the 2nd and 3rd arguments ($row->ip_address_01 = $ipONactivate is assigning $ipONactivate to $row->ip_address_01, not making an array). You're also trying to use the column value of a previous query (eg. $row->ip_address_01) as the column name.

You need to format your arguments like this:

$wpdb->update(
    $ipn_table,
    array( 
        'ip_address_01' => $ipONactivate
    ),
    array(
        'payer_email' => $serial,
        'item_name'   => $product,
    )
);

That function will update the ip_address_01 column, of any row where payer_email is $serial and item_name is $product. The equivalent SQL would be:

$sql = "UPDATE {$ipn_table} SET ip_address_01='{$ipONactivate}' WHERE payer_email='{$serial}' AND item_name='{$product}'";

The advantage of the function is that it saves you having to write the full query (you only need the column names and values) and it also properly prepares and escapes the values for you, so you don't end vulnerable to SQL injections (which you would be if you used my SQL example as-is).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748912060a314752.html

最新回复(0)