提问人:user22881435 提问时间:11/15/2023 最后编辑:user22881435 更新时间:11/15/2023 访问量:25
按钮、SWAL 和更改 DB 中的值 [已关闭]
button, swal and changing value in db [closed]
问:
我需要更改表用户中 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 天后睡觉,因为我几乎看不到,但我需要答案。可能丢失了我的最后一个逻辑单元。谢谢
答:
0赞
ZeNix
11/15/2023
#1
我想你只是一个错别字问题。在您的 JS 脚本中,您正在尝试发送 ,但在 PHP 中,您期望在超全局上遇到,请像这样更改您的代码:userId: userId
userID
$_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();
}
评论
userId: userId
userID
$userId = $_POST['userID'];