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.com/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'
1

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).

1
  • Thanks for the explanation Jacob. Both yourself and Krzysiek have been very patient and helpful with slow learners, and i think we appreciate the explanations MORE simply because, i feel, documentation is a little 'Greek' sometimes, depending on the source (no pun) and different ways to do the same thing from different people gets confusing on the web but you explained it well. I'm not even going to try the code till i read and compare and understand it. Again, Thank you. :)
    – Schmutly
    Feb 13 '19 at 22:37

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.