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

时间:2017-07-20 09:18:20

标签: php mysql database html5 phpmyadmin

目标:

我试图通过按下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>

PHP代码减去0.05(文件名: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 | first_name | last_name |电子邮件|密码| cash_amount |哈希|活性

额外问题:

在我网站的主页上显示现金金额。一旦更改它就不会更新,直到我退出并重新登录。有没有我可以放在顶级的PHP代码,如&#34;会话重启&#34;每次打开页面时检查新的金额?

谢谢:

非常感谢你的帮助,因为你可以看到我在这方面有点像菜鸟:)

2 个答案:

答案 0 :(得分:1)

在查询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();
?>

答案 1 :(得分:0)

您需要使用WHERE子句

将更新限制为仅一个用户
$sql = "UPDATE users SET cash_amount = $newAmount WHERE user_id = $userid LIMIT 1";

为了安全起见,增加了LIMIT。