AgataUrsula
AgataUrsula

Reputation: 13

Storing integer value in Wordpress db via javaScript and PHP

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.

Upvotes: 0

Views: 96

Answers (0)

Related Questions