2

Im trying to retrieve the values from MySQL database and convert it into int and than adding 3 to it and than trying to store the update int into database.Im not getting any error or anything.Initial value that i retrieve from database is 5 and than im trying to add 3 and than update the database.Now the updated value should be 8 but its not instead it just 3.I have no clue about what am i doing wrong so please help me out.

My php code from the index.php is following:

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);
            } 
            }

Here is the code of the function from DB_functions.php which handles update query:

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);
        }

Thank You!!

1 Answer 1

2

You should save yourself the headache and do it in one query:

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

This will automatically increment your column karma_points for user $username with the value of $karma_points. Instead of doing the math in PHP and sending it back to MySQL, just do it in MySQL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.