按钮、SWAL 和更改 DB 中的值 [已关闭]

button, swal and changing value in db [closed]

提问人:user22881435 提问时间:11/15/2023 最后编辑:user22881435 更新时间:11/15/2023 访问量:25

问:


这个问题是由错别字或无法再现的问题引起的。虽然类似的问题可能在这里是主题,但这个问题的解决方式不太可能帮助未来的读者。

5天前关闭。

我需要更改表用户中 vyber 列的值。默认情况下为 0。当有人单击此按钮时,它应该将 0 更改为 1 $id 编辑:ID -> id 中有拼写错误,但它没有更改 db 中的值。

<span>
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion7" href="#collapse_7" data-id="<?= $id ?>">
    button
</a>
</span>
<script>
    $(document).ready(function() {
        $("body").on("click", ".accordion-toggle", function (e) {
            e.preventDefault();
            console.log("Button clicked");

            
            var userId = $(this).data('id');

            Swal.fire({
                title: 'Jste si jistí ?',
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Ano',
                cancelButtonText: 'Ne'
            }).then((result) => {
                if (result.value) {
                    // User clicked Yes
                    $.ajax({
                        url: 'assets/php/action.php', 
                        method: 'post',
                        data: { action: 'updateInvestmentStatus', userId: userId },
                        success: function (response) {
                            Swal.fire({
                                title: 'Žádost odeslána',
                                text: response,
                                type: 'success'
                            });
                        },
                        error: function (error) {
                            Swal.fire({
                                title: 'Error',
                                text: 'An error occurred while processing your request.',
                                type: 'error'
                            });
                        }
                    });
                }
            });
        });
    });
</script>

我的操作

if (isset($_POST['action']) && $_POST['action'] == 'updateInvestmentStatus') {
    $userId = $_POST['userID'];

    try {
        
        $auth->updateInvestmentStatus($userId);
        echo json_encode(['message' => 'Investment status updated successfully', 'userId' => $userId]);
    } catch (Exception $e) {
        echo json_encode(['error' => 'Error updating investment status: ' . $e->getMessage()]);
    }

    exit(); 
}

我的功能

public function updateInvestmentStatus($userId)
{
    $sql = "UPDATE users SET vyber = 1 WHERE id = :userId";
    $stmt = $this->conn->prepare($sql);
    $stmt->bindParam(':userId', $userId, PDO::PARAM_INT);

    if ($stmt->execute()) {
        return "Investment status updated successfully.";
    } else {
        return "Error updating investment status: " . print_r($stmt->errorInfo(), true);
    }
}

有人可以告诉我它在哪里或是什么吗?我可能必须在 3 天后睡觉,因为我几乎看不到,但我需要答案。可能丢失了我的最后一个逻辑单元。谢谢

php mysql pdo

评论

0赞 Barmar 11/15/2023
“告诉我在哪里或是什么”什么?代码对我来说看起来没问题,有什么问题?你做了什么调试?
1赞 ZeNix 11/15/2023
我认为这是一个错别字问题,您正在发送您的 JS 脚本,但试图在超全局中阅读。userId: userIduserID$userId = $_POST['userID'];
0赞 user22881435 11/15/2023
是的,谢谢,ID中有错别字,我没有看到。但即使是现在,如果执行此操作,它也不会将值从 0 更改为 1。

答:

0赞 ZeNix 11/15/2023 #1

我想你只是一个错别字问题。在您的 JS 脚本中,您正在尝试发送 ,但在 PHP 中,您期望在超全局上遇到,请像这样更改您的代码:userId: userIduserID$_POST

if (isset($_POST['action']) && $_POST['action'] == 'updateInvestmentStatus') {
    $userId = $_POST['userId']; // Your typo was here.

    try {
        
        $auth->updateInvestmentStatus($userId);
        echo json_encode(['message' => 'Investment status updated successfully', 'userId' => $userId]);
    } catch (Exception $e) {
        echo json_encode(['error' => 'Error updating investment status: ' . $e->getMessage()]);
    }

    exit(); 
}