2010-03-15 5 views
4

이 함수는 데이터베이스의 값을 업데이트해야합니다. 그것은 여기에 사용되는이 기능에 문제가 있습니다. 코드가 실행되지 않습니다.

//Functions 
//Function to Update users networth 
function update_net($name) 
    { 
    //Get worth & balance at the time 
    $sql_to_get_worth_balance = "SELECT * FROM user WHERE username = '$name'"; 
    $sql_query = mysql_query($sql_to_get_worth_balance); 
    while ($rows = mysql_fetch_assoc($sql_query)) 
    { 
    $worth = $rows['worth']; 
    $balance_ = $rows['cash_balance']; 
    } 
    //Get net_worth now 
    $new_net_worth = $worth + $balance; 
    //Update net_worth 
    $sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'"; 
    $sql_worth_query = mysql_query($sql_worth); 
    } 

:

//Get username 
$username = $_SESSION['username']; 

if (isset($username)) 
{ 
    //Update networth 
    $update_worth = update_net($username); 

답변

6

당신은 아마이 쿼리의 끝에 WHERE 절을 원하는

$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth' WHERE username = '$name'; 
0

당신이 트랜잭션을 커밋해야 할 수 있음 여기

코드인가?

$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'"; 

예를 들어, - :

3
  1. 당신은 업데이트 쿼리의 경우 이름 = $ 이름 부분 잊고있어 (전체 테이블을 업데이트 할 것입니다!)
  2. 나는 당신의 SQL 때문에 사용자가 데이터를 입력하여 $ 이름을 보유 할 수 없다 희망 주입에 가역적이다.
1

미정 :

//Update net_worth 
$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'"; 
$sql_worth_query = mysql_query($sql_worth); 

읽어야합니다

//Update net_worth 
$sql_for_new_worth = "UPDATE user SET net_worth = '$new_net_worth'"; 
$sql_worth_query = mysql_query($sql_for_new_worth); 
관련 문제