gpt4 book ai didi

php - 尝试从数据库中的列中减去金额,显示所有帐户的金额

转载 作者:行者123 更新时间:2023-11-29 10:42:34 24 4
gpt4 key购买 nike

目标:

我试图通过按下 html 按钮从数据库中的列中减去 0.05。

<小时/>

出了什么问题:

一切正常,它正在进行正确的计算并从我的帐户中删除正确的金额,但它正在数据库中的每个帐户上显示新的金额。

<小时/>

示例:

我的帐户中有 10 个,一旦按下按钮,我的帐户中现在有 9.95 个。不幸的是,现在每个人的账户里也有 9.95!

<小时/>

代码:

用于 onclick 的 HTML 按钮和脚本:

<button id="playbutton" onclick="myAjax();location.href='5game.html'">Play (-5&#162;)</button>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : { },
url : 'subtract5.php', // <=== CALL THE PHP FUNCTION HERE.
success: function ( data ) {
alert( data ); // <=== VALUE RETURNED FROM FUNCTION.
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>

减 0.05 的 PHP 代码(文件名:subtract5.php):

<?php
session_start();

$servername = "localhost";
$username = "my username"; <not actually this, just confidential
$password = "my password"; <not actually this, just confidential
$dbname = "accounts";
$cash_amount = $_SESSION['cash_amount'];

// Create connection

$userid = $_SESSION['id'];

// You must enter the user's id here. /\

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Fetch the existing value of the cash_amount against that particular user here. You can use the SELECT cash_amount from users where userid = $userid

$newAmount = $cash_amount - 0.05;

$sql = "UPDATE users SET cash_amount = $newAmount";
$result = $conn->query($sql);

if($result)
{
echo "Query Executed Successfully!";
}
else
{
echo mysqli_error($conn);
}

$conn->close();
?>
<小时/>

预测:

我认为这与用户 ID 字段有关,我不确定要在其中输入什么。

<小时/>

数据库布局:数据库:账户,表:用户,列: id |名字 |姓氏 |电子邮件 |密码 |现金金额 |哈希 |活跃

<小时/>

额外问题:

在我的网站主页上,显示了现金金额。一旦更改,它就不会更新,直到我注销并再次登录。我是否可以在顶部 php 代码中放入“ session 重新启动”之类的内容,以在每次打开页面时检查新金额?

<小时/>

谢谢:

非常感谢您的帮助,正如你所看到的,我在这方面有点菜鸟:)

最佳答案

在查询 WHERE id = $userid 的 where 子句中:

<?php
session_start();

$servername = "localhost";
$username = "my username"; <not actually this, just confidential
$password = "my password"; <not actually this, just confidential
$dbname = "accounts";
$cash_amount = $_SESSION['cash_amount'];

// Create connection

$userid = $_SESSION['id'];

// You must enter the user's id here. /\

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Fetch the existing value of the cash_amount against that particular user here. You can use the SELECT cash_amount from users where userid = $userid

$newAmount = $cash_amount - 0.05;

$sql = "UPDATE users SET cash_amount = $newAmount WHERE id = $userid";
$result = $conn->query($sql);

if($result)
{
echo "Query Executed Successfully!";
}
else
{
echo mysqli_error($conn);
}

$conn->close();
?>

关于php - 尝试从数据库中的列中减去金额,显示所有帐户的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45210469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com