0

I am trying to do an UPDATE to a table in PHP while using jQuery Ajax. The jQuery Ajax code calls the php file where a function is executed.

That function makes an UPDATE query with mysqli_query method. However, it doesn't work.

I've tried multiple solutions and none of them worked. The status_player1 column is a VARCHAR(20) and the id column is INTEGER.

When making the exact output I get of the full query in my database manually it works.

Here's the jQuery code:

   counterConfirm = 0;
   $(document).on("click", "#confirmButton", function(){
        counterConfirm++;

        if (counterConfirm == 1) {
            $.ajax({
                type: "POST",
                url:"../php_includes/game_calc.php",
                data: {"insert": counterConfirm},
                success: function(data){
                    console.log("successful");
                    console.log(data);
                }
            })
        }
    });

And here's the PHP code:

    include('openconn.php');

    if(isset($_POST["insert"])) {
        addInsert($_POST["insert"]);
    }

    function addInsert($count) {
        $id = intval($_SESSION["game_id"]);

        if($count == 1) {
            $query = "UPDATE games SET status_player1='R' WHERE id='{$id}';";
            mysqli_query($conn, $query);
            echo $query;
        }

    }

After executing the code, I get the succesfulstring on my console, but my database isn't updated.

Inês
  • 423
  • 1
  • 5
  • 7
  • 5
    Learn about prepared statements to prevent sql injection – Jens May 18 '19 at 14:55
  • In your browser's debugging tools, check the network tab. What is the response from the server? Does it contain the query you expect? Is `$count` equal to 1 at all? Have you confirmed that you're even executing the query? Where specifically is this failing? Also, check for errors with `mysqli_error`. – David May 18 '19 at 14:55
  • Do your POST parameters show in the PHP when you `print_r` them? – Intel May 18 '19 at 14:58
  • 1
    The ajax.success event is always fired when an 200 header is given. Your exception is not throwing an 404 or 304 header code, so its always "success". – Richard May 18 '19 at 15:13

0 Answers0