1

Im new at Android. I'm trying to fetch some data from localhost server. My query is running perfectly on phpMyAdmin But I facing error in api. I have very little knowledge about Php so did not get what the issue is.

Code:

public function saveUserProgress($user_id,$course_id,$topic_id,$quiz_marks){

        $output = $this->con->prepare("INSERT INTO user_progress (user_id, course_id, topic_id,quiz_marks)
        VALUES (?, ?, ?,?)
        ON DUPLICATE KEY UPDATE
            user_id=?, course_id=?, topic_id=?, quiz_marks = quiz_marks + ?");

        $output->bind_param("iiii",$user_id,$course_id,$topic_id,$quiz_marks);

        if($output->execute()){
            return  PROGRESS_SAVED;
        }else{
            return ERROR_OCCUR;
        }
    }

Error:

{"error":true,"message":403}
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement 
4
  • so will i pass these 4 variables again ?@Dharman Commented Sep 2, 2019 at 18:07
  • @Dharman 4 variables in insert statement is same as 4 variables in On Duplicate Key Update statement Commented Sep 2, 2019 at 18:12
  • Which is what is confusing me. What is your unique key? Commented Sep 2, 2019 at 18:12
  • user_id and course_id is set unique as same user not have same course twice @Dharman Commented Sep 2, 2019 at 18:17

1 Answer 1

4

If as you said your unique key is user_id and course_id then you do not need to update them on duplicate key. You only need to update the remaining 2 values. Together with the 4 you wanted to add it makes 6 placeholders, so you need to bind 6 variables.

$output = $this->con->prepare("INSERT INTO user_progress (user_id, course_id, topic_id,quiz_marks)
    VALUES (?, ?, ?, ?)
    ON DUPLICATE KEY UPDATE 
        topic_id=?, quiz_marks = quiz_marks + ?");

$output->bind_param("iiiiii", $user_id, $course_id, $topic_id, $quiz_marks, $topic_id, $quiz_marks);
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.