我试图在按下按钮后从数据库中的玩家现金金额减去0.05。这是我到目前为止所得到的。
我的数据库:
数据库名称:帐户
表:用户
我想影响的专栏: cash_amount
HTML:
<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>
<button id="playbutton" onclick="myAjax();location.href='5game.html'">Play (-5¢)</button>
Php档案:( subtract5.php)
<?php
UPDATE `accounts`.`users` SET `cash_amount` = '`cash_amount` - 0.05'
感谢您的帮助,我有点像菜鸟。)
答案 0 :(得分:0)
以下代码,但是不要完全复制粘贴它,读取注释有一些变量,你可能需要更改并从数据库中获取。
<?php
$servername = "servarname";
$username = "username";
$password = "password";
$dbname = "dbName";
// Create connection
$userid = // 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 = $previousAmount - 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();
?>
答案 1 :(得分:0)
尝试执行该请求。这只不过是一个没有引号的字符串。我确信PHP认为它是一个错误。
在尝试编写项目代码之前,您应该考虑学习PHP和MySQL。