2016-11-15 71 views
0

該任務的想法是允許用戶向他們的帳戶添加和從他們的帳戶中提取「錢」。問題是,我可以加錢,但我不能收回沒有得到正確的價值回報

$funds = $_POST['funds']; 
$withdraw_or_add = $_POST['list']; 

if($withdraw_or_add == "add") 
{ 
    $sql = "UPDATE users SET userFunds = '".$funds."' WHERE userId = 1"; 
} 
else 
{ 
    $info = mysql_query("SELECT * FROM users WHERE userId = '1'"); 
    $info = mysql_fetch_assoc($info); 
    $new_fund = $info['userFunds'] - $funds; 
    $sql = "UPDATE users SET userFunds = '".$new_fund."' WHERE userId = 1"; 
} 


mysql_select_db('details_db'); 
$retval = mysql_query($sql, $conn); 

if(! $retval) { 
    die('Could not update data: ' . mysql_error()); 
} 

echo "Updated data successfully\n"; 

mysql_close($conn); 

因此,舉例來說,假設$fund = 5$info['userFunds'] = 20那麼變量$new_fund應該15。但相反,它等於-5。如果任何人都可以幫助它,將不勝感激。

+6

***請[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。*** [這些擴展名](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[prepared](http://en.wikipedia.org/wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart。 prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+5

[Little Bobby](http://bobby-tables.com/)說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can- I-防止-SQL注入式-PHP)***。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! *** SQL注入!*** *這不僅僅是用於早餐!* –

+2

您需要先連接到您的數據庫,然後才能查詢,以及$ conn'與此窗體一起定義在哪裏? –

回答

1

首先頂部的頁面你把用過的數據庫連接相關的代碼:

$conn = mysql_connect('localhost', 'user', 'pass'); 
mysql_select_db('details_db'); 

然後波紋管和後mysql_

$funds = $_POST['funds']; 
$withdraw_or_add = $_POST['list']; 

if($withdraw_or_add == "add") 
{ 
    $sql = "UPDATE users SET userFunds = '".$funds."' WHERE userId = 1"; 
} 
else 
{ 
    $info = mysql_query("SELECT * FROM users WHERE userId = '1'"); 
    $info = mysql_fetch_assoc($info); 
    $new_fund = $info['userFunds'] - $funds; 
    $sql = "UPDATE users SET userFunds = '".$new_fund."' WHERE userId = 1"; 
} 


//mysql_select_db('details_db'); 
$retval = mysql_query($sql, $conn); 

if(! $retval) { 
    die('Could not update data: ' . mysql_error()); 
} 

echo "Updated data successfully\n"; 

mysql_close($conn); 

注意去除mysql_select_db('details_db');線:請停止使用mysql_*功能。 mysql_*extensions已在PHP 7中刪除。請使用PDOMySQLi

+0

這有效。非常感謝你! –