2016-08-03 22 views
1

我有一個PHP表單來更新MySQL數據庫。我想爲行中的當前積分添加積分。基本的數學PHP表單來更新MYSQL行

當前積分50再添50個積分= 100

這是我當前字符串(編者)

$sql = sprintf("UPDATE h_clients ". "SET credits = $credits + '%s'" . "WHERE id = $id", 

Currently this works but it doesn't add the new credits with the current credits in DB 

Here is the full code: 

$sql = sprintf("UPDATE h_clients ". "SET credits = $credits + '%s'" . "WHERE id = $id", 

mysql_real_escape_string($credit), 
       mysql_real_escape_string($id)); 

      if (mysql_query($sql, $con)) { 
       $insertSuccessful = true; 
      } else { 
       echo $sql; 
       echo "\n" . mysql_error($con); 
       echo "mysql err no : " . mysql_errno($con); 
      } 
    } 

    } 

    return $insertSuccessful; 
+0

試$ SQL = sprintf的( 「UPDATE h_clients」。 「SET學分=學分+ '%s' 的。」 「WHERE ID = $ id爲」 有錯誤的列名, – cmnardi

回答

0

看來你在分配學分

"UPDATE h_clients SET credits = credits + $credits WHERE id = $id", 
+0

感謝這工作,認爲許多「」在我的代碼和調用目前在數據庫中的信用,而不是%S我試過 –

+0

那麼如果工作......並且我的回答是正確的,請將其標記爲已接受.. – scaisEdge

1

如果你只是增加了一個儲存在數據庫中的值,你應該能夠這樣做:

$sql = sprintf("UPDATE h_clients SET credits = credits + $credits WHERE id = $id", 

mysql_real_escape_string($credit), 
      mysql_real_escape_string($id)); 

請澄清,如果這不是你想要的。