我试图在[share]表中插入语句并在[balance]表中更新语句,如果余额表中的余额大于或等于75,但它总是会进入else语句,即使它满足必要条件balance>=75
以下是我的代码:
<?php
if (isset($_POST['submit'])) {
$site = htmlspecialchars($_POST['site']);
$me=$current_user->user_login;
$today = date('Y-m-d');
$me=$current_user->user_login;
$table_balance = $wpdb->prefix . "balance";
$result = $wpdb->get_results(
"SELECT Balance FROM $table_balance WHERE User= '$me'"
);
if ($result && mysql_num_rows($result)>=75) {
$random = uniqid();
$table_share = $wpdb->prefix . "share";
$wpdb->insert( $table_share, array(
'id' => $random,
'purchasedate' => $today,
'purchasevalue' => 75,
'offervalue' => 0,
'status' => Active,
'user' => $me,
'monthlyearnings' => 2,
));
$sum=75;
$table_balance = $wpdb->prefix . "balance";
$wpdb->update(
"$table_balance SET Balance=Balance-$sum WHERE User = '$me'"
);
echo "Purchased made successfully";
exit();
} else {
echo "sorry!you dont have required amount.pls add required amount";
}
?>
答案 0 :(得分:1)
您使用错误的函数来检查数据库的值,mysql_num_rows()
返回查询返回的行数,在这种情况下可能为1,小于75 else
得到总是执行。
要获得余额,您需要使用fetch_row
,如下所示:
if ($result && ($row = $result->fetch_row()) && $row[0]>=75)
编辑:我使用标准版mysqli_stmt::get_result
放置您使用的内容,但您使用的是wpdb::get_results
,所以您必须使用:
if ($result && $result[0]->Balance>=75)