2017-07-20 24 views
0

目的量:試圖在數據庫中列減去量,顯示出所有帳戶

我試圖減去一列0.05在我的數據庫上的HTML按鈕即可。


什麼錯:

一切工作正常,它正在正確的計算,並從我的帳戶刪除正確的金額,但在數據庫中顯示的每個帳戶,新的金額。


例子:

我有10個在我的帳戶,有一次我按下按鈕,我現在有9.95我的帳戶。不幸的是,現在每個人的賬戶裏都有9.95!


代碼:

HTML按鈕和腳本的onclick:

<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 | hash |積極


額外的問題:

在我的網站的主要頁面,它顯示的現金數額。一旦它被更改,它不會更新,直到我註銷並重新登錄。是否有一件事我可以放在頂部的PHP代碼,如「會話重啓」,每次打開頁面時檢查新的數量?


感謝:

感謝這麼多的幫助,因爲你可以看到我是怎樣的一個小白在這個:)

+0

您可以發送帶有POST的用戶id到您的php腳本,並在您的查詢中的WHERE子句中使用它 –

回答

1

在查詢的WHERE子句WHERE ID = $用戶名:

<?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(); 
?> 
+0

由於某種原因,它不會減去任何東西,只是重定向到其他頁面。 – Patrick

+0

檢查用戶標識是否即將到來@Patrick? –

+0

我該如何檢查? – Patrick

0

則需要使用限制更新只有一個用戶 WHERE子句

$sql = "UPDATE users SET cash_amount = $newAmount WHERE user_id = $userid LIMIT 1"; 

LIMIT是爲了安全而添加的。

相關問題