0

I'm running into a problem where I can't find a solution. I have a little game plugin which is written in JavaScript. Now I would like to store the highscore in my wordpress database. Therefore I included a jQuery function in my game which is passing the score to a php file. I'm able to retrieve values from my database but weren't able to update them. I get an 500 error in the browsers console. I first had an 404 error but I solved it with changing the url.

My javascript code:

function () {
            jQuery.ajax({
                url: 'wp-content/plugins/dinosaur-game/update-score.php',
                type: "POST",
                data: {data: this.highestScore},
                
            });                        
            
        }

My php code:

<?php
    global $wpdb;
    $new_score = stripslashes_deep($_POST['data']);
    
    $table_name = 'wp_users'; 
    $data = array('game_score' => $new_score); 
    $where = array('ID' => 5); 
    //$wpdb->update($table_name, $data, $where); // tried this one - 500 error
    $wpdb->query("UPDATE 'wp_users' SET 'game_score' = $new_score WHERE 'ID' = 5");
?>

I used the wpdb update function and also tried to write the whole query but both produced the 500 error and the value does not update in my database. I also tried to cast the value with (int) without success.

  • In your ajax, you are passing `data` by POST, but in your WP, you want to get something like `$_POST['integerData']` ? – Ken Lee May 05 '23 at 14:02
  • Dump your `$_post` variable in there, to see if it's actually in it ... If you submit nothing or an error, you get issues. Never trust input as well like this, it's manipulatable. Also do a dump on the query, and check if you paste that query in say phpmyadmin if you get results. It's mostly debugging 101. Also you may wanna build checks to see if the value is filled. – Dorvalla May 05 '23 at 14:11
  • 1
    @KenLee your right, I fixed that but still no improvement. But appreciate the help! – AgataUrsula May 05 '23 at 14:15

0 Answers0