You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PDO实现按用户ID计算inv并更新zhod字段的功能求助

问题分析与修正方案

你的代码存在几个关键问题导致功能失效:

  • updateZhod($id)方法要求传入用户ID,但调用时未传参,触发参数缺失错误
  • AJAX请求指定dataType: 'json',但后端返回纯文本,会导致解析失败
  • 当前方法仅能更新单个用户,若要批量更新所有已验证用户的zhod,SQL语句需调整
  • 前端success回调中使用response.newValue,但后端未返回该字段

简化后的完整实现

1. 后端PHP修正(admin-action.php)

批量更新所有已验证用户的Zhod

修改updateZhod方法,去掉ID参数,直接批量更新符合条件的用户:

public function updateZhod() {
    // 批量更新所有已验证用户的zhod字段
    $sql = "UPDATE users 
            SET zhod = inv * 0.015 
            WHERE verified = 1";
    $stmt = $this->conn->prepare($sql);
    return $stmt->execute(); // 返回执行结果
}

处理AJAX请求的逻辑

修正updateZhod请求的处理逻辑,返回标准JSON格式:

if (isset($_POST['action']) && $_POST['action'] == 'updateZhod') {
    $updateResult = $admin->updateZhod();
    // 返回JSON格式响应
    echo json_encode([
        'success' => $updateResult,
        'message' => $updateResult ? 'Zhod values updated successfully!' : 'Failed to update zhod values.'
    ]);
    exit;
}

// 保留原有的fetchAllUsersInv逻辑(如需展示数据)
if (isset($_POST['action']) && $_POST['action'] == 'fetchAllUsersInv') {
    $output = '';
    $data = $admin->fetchAllUsersInv(0);
    if ($data) {
        $output .= '<table class="table table-striped table-bordered text-center">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Inv</th>
                                <th>Zhod</th>
                            </tr>
                        </thead>
                        <tbody>';

        foreach ($data as $row) {
            $zhodValue = $row['inv'] * 0.015;

            $output .= '<tr>
                            <td>' . $row['id'] . '</td>
                            <td>' . $row['inv'] . '</td>
                            <td>' . number_format($zhodValue, 2) . '</td>
                        </tr>';
        }

        $output .= '</tbody>
                    </table>';

        echo $output;
    } else {
        echo '<h3 class="text-center text-secondary">:( No inv</h3>';
    }
    exit;
}

2. 前端AJAX修正

调整回调逻辑,适配后端返回的JSON数据:

$(document).ready(function() {
    $("#zhodLed").click(function() {
        $.ajax({
            url: 'assets/php/admin-action.php',
            type: 'POST',
            data: { action: 'updateZhod' },
            dataType: 'json',
            success: function(response) {
                if (response.success) {
                    alert(response.message);
                    // 可选:更新后自动刷新表格数据
                    // $("#table-container").load("assets/php/admin-action.php", { action: "fetchAllUsersInv" });
                } else {
                    alert(response.message);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.error("AJAX Error:", textStatus, errorThrown);
                alert("Error: Unable to make the AJAX request.");
            }
        });
    });
});

关键优化点

  • 批量更新直接用SQL完成,无需循环处理每个用户,性能更优
  • 统一使用JSON格式返回响应,避免前端解析错误
  • 增强错误反馈的可读性
  • 可选:更新后自动刷新用户数据表格,提升交互体验

内容的提问来源于stack exchange,提问作者user22881435

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 08:43:33