Trying to get variable from WP table and toggle its value
-
26-04-2021 - |
Вопрос
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.
Решение
There are two issues I see in your code:
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 ) );
Your
if
should use the equality operator (==
or===
) and not the basic assignment operator (=
).So for examples, use
if ($StatusCheck == "Completed")
andif ($StatusCheck == "Refunded")
instead of what you currently have in your code.