Trying to get variable from WP table and toggle its value

I have a query setup in a function to grab the payment status from a table in wordpress using a search string.
It will usually be ‘Completed’ (returned from paypal IPN).
All i’m trying to do is allow a local option to change the value string to another value and back again.
This is what i have so far:

 <!---TOGGLE PAYMENT STATUS--->

<?php function toggle_payment_status ()
        { 
           
 global $wpdb;
 $ipn_tables = $wpdb->prefix ."ipn_data_tbl";
 $searchIP = $_POST["searchIP"];
 
 $StatusCheck = $wpdb->get_var( $wpdb->prepare("SELECT payment_status FROM {$ipn_tables} WHERE serial_no = %s "),$searchIP);


  if ($StatusCheck = "Completed")   {
       $StatusCheck = "Refunded";
      } 
  
  else if ($StatusCheck = "Refunded")    {
      $StatusCheck = "Completed";
      }
                             
      $wpdb->update($ipn_tables, array(
          'payment_status' => $StatusCheck
                             ) , array(
            'serial_no' => $searchIP,
    ));
 }

When i run it the completed string changes to Refunded. A check in the Database shows the change. Great. So the $statuscheck should be this new ‘changed’ value of Refunded…but i don’t understand why the if statements are failing.
I did try elseif but not working either.
Could someone steer Mr dumbo in the right direction and explain what i did wrong?
Too much coffee & late nights hasn’t help. Thank you.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are two issues I see in your code:

  1. The $searchIP is actually outside of the $wpdb->prepare(): (I didn’t use the full query so that you’d see the issue clearer)
    $StatusCheck = $wpdb->get_var( $wpdb->prepare("SELECT payment_status ..."),$searchIP);
    

    So you should correct that, like so: (reindented for clarity)

    $StatusCheck = $wpdb->get_var( $wpdb->prepare( "
        SELECT payment_status FROM {$ipn_tables}
        WHERE serial_no = %s
    ", $searchIP ) );
    
  2. Your if should use the equality operator (== or ===) and not the basic assignment operator (=).

    So for examples, use if ($StatusCheck == "Completed") and if ($StatusCheck == "Refunded") instead of what you currently have in your code.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x