2012-07-22 2 views
2

임 MySQL 데이터베이스에서 값을 검색하고 그것을 int로 변환하고 그것에 3을 추가하고 데이터베이스에 업데이트 int를 저장하려고 시도하는 것보다 오류가 발생하지 않습니다. 데이터베이스에서 검색하는 초기 값은 5입니다. 메신저보다 3을 추가하려고 database.Now 업데이트 된 값은 8이되어야하지만 그게 아니라 그냥 3.I 내가 잘못하고있는 일에 대한 단서가 없다 그래서 나를 도와주세요.MySQL 쿼리에서 얻은 문자열/val을 PHP에서 int로 변환 하시겠습니까?

의 index.php에서 내 PHP 코드는 다음입니다 :

여기
else if ($tag == 'addQuestion'){ 
       $username = mysql_real_escape_string($_POST['username']); 
       $question = mysql_real_escape_string($_POST['question']); 
       $tag1 = mysql_real_escape_string($_POST['tag1']); 
       $tag2 = mysql_real_escape_string($_POST['tag2']); 
       $tag3 = mysql_real_escape_string($_POST['tag3']); 
       $time = $_POST['time']; 
      $addQu = $db->addQuestion($username, $question, $tag1, $tag2, $tag3,$time); 
     if($addQu){ 
      $q_id = $addQu["id"]; 
      $addQTA = $db->addQTA($username,$q_id,$question,$tag1,$tag2,$tag3); 
      if($addQTA){ 
       $getKP= $db->getKP($username); 
       if($getKP){ 

              //Having trouble at this part 
        $kp = (int)$getKP['karma_points']; 
        $ask_question_points = $kp + 3; 

        $updateKP= $db->updateKP($username,$ask_question_points); 
         if($updateKP){ 
          $response["error"] =1; 
          $response["msg"] = "updateKP in AddQuestion Succesfull"; 
          echo json_encode($response); 
        } 
        else{ 
         $response["error"] =1; 
         $response["error_msg"] = "Error updateKP in AddQuestion"; 
         echo json_encode($response); 
         } 
        } 
        else{ 
         $response["error"] =1; 
         $response["error_msg"] = "Error inserting getKP in AddQuestion"; 
         echo json_encode($response); 
         } 
       } 
      else{ 
       $response["error"] =1; 
       $response["error_msg"] = "Error inserting QTA"; 
       echo json_encode($response); 
       } 
     }else{ 
      $response["error"] =1; 
      $response["error_msg"] = "Error inserting question"; 
      echo json_encode($response); 
      } 
      } 

가 업데이트 쿼리를 처리 DB_functions.php에서 함수의 코드입니다 :

public function updateKP($username,$karma_points){ 
     $result = mysql_query("UPDATE users SET karma_points = '$karma_points' WHERE username = '$username'") or die(mysql_error()); 
     return($result); 
     } 

가 감사합니다!

답변

2

당신은 자신에게 두통을 저장 한 쿼리를 수행해야합니다

$result = mysql_query("UPDATE users SET karma_points = karma_points + $karma_points WHERE username = '$username'") or die(mysql_error()); 

이 자동으로 $karma_points의 값을 사용자 $username에 대한 귀하의 열 karma_points을 증가합니다. PHP에서 수학을 수행하고 MySQL로 다시 보내는 대신 MySQL에서 수행하십시오.

+1

response.it에 감사드립니다. – Viking

+0

언제나 듣기 좋습니다! –