PDO 函数插入和乘法

问题描述 投票:0回答:1

我正在尝试创建一个函数,将 inv 列中的每一行乘以用户 id,然后将其乘以 0,015,然后将此值插入到具有相同用户 id 的 zhod 中。

我有这个,但我已经迷路了。如果有人可以帮助或简化它,我会很高兴。

public function updateZhod($id) {
   $sql = "UPDATE users SET zhod = inv * 0.015 WHERE id = :id AND verified = 1";
    $stmt = $this->conn->prepare($sql);
    $stmt->bindParam(':id', $id);
    $stmt->execute();
}
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>' . $zhodValue . '</td>
                        </tr>';
        }

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

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


if (isset($_POST['action']) && $_POST['action'] == 'updateZhod') {
    $admin->updateZhod();

    echo 'Zhod values updated successfully!';
}
<script>
    $(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) {
                        $("#invValue").text(response.newValue.toFixed(2) + " ...");
                    } else {
                        alert("Error: Failed to update zhod value.");
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.error("AJAX Error:", textStatus, errorThrown);
                    alert("Error: Unable to make the AJAX request.");
                }
            });
        });
    });
</script>

这应该是一个简单的函数,从 inv 中取出一个值乘以 0,015 并将其插入到 zhod 中,但它可能落后于我的技能

javascript php pdo sql-insert
1个回答
0
投票

通过 userId 及其 id 参数在循环中移动管理块:

$admin->updateZhod($row['id']);

顺便说一句:您需要一个标志来将该值相乘一次,以防止每个操作多次相乘。

© www.soinside.com 2019 - 2024. All rights reserved.