Home > database >  Changing database entry given player ID from Javascript
Changing database entry given player ID from Javascript

Time:06-01

I am writing a javascript game with three different levels which can be accessed from an html form in the index.html page. I also have a list of players stored in a database and I need to:

  1. Prevent players from accessing levels they have already completed, and
  2. Prevent players from starting a new level until every other player has finished the current level

The way I thought to achieve this was to create two database columns named "level_1" and "level_2" which are set to 0 at the beginning of the level and should change to 1 once the level is complete, like so:

*beginning of game
player_ID   level_1  level_2
1           0            0
2           0            0
3           0            0

*level one is completed

player_ID   level_1  level_2
1           1            0
2           1            0
3           1            0

*level two is completed

player_ID   level_1  level_2
1           1            1
2           1            1
3           1            1

When each entry in column "level_1" is changed to 1 I would disable the level 1 option in the html form and "unlock" the level 2 option. The same applies to the second and third levels.

However, I am unsure how to do this using javascript and php. I have a counter in javascript that records the player moves. When the moves go to zero the level is over and the final score is displayed:

if (moves==0){
    document.getElementById('endgame_screen').style.display = "flex";
    document.getElementById('score').innerHTML = points
}

I also stored the level and id form choices in the same javascript file:

var level = sessionStorage.getItem('level');
var id = sessionStorage.getItem('id');

What I have been struggling to do is opening the php file from the game javascript file and updating the database entries based on level and id. I know the best way would be to fetch the php file like:

fetch("levels.php", {method="POST"}

but then I get stuck as I am not very familiar with the SQL and PHP syntaxes and I don't know how to send the level and id data through PHP, nor how to update the database. I am also not sure how to then disable the form choices which are on another html page (my guess would be to trigger another PHP file when the form is opened that checks whether there are any zeros in the two database columns and updates the form accordingly).

Any help is appreciated!

CodePudding user response:

You will have to write al least a minimal script in PHP to interact with the database.

The bare minimum would be:

<?php
$con = mysqli_connect("localhost","user","password","dbname");

mysqli_query($con, "UPDATE table SET level=1 WHERE user='johndoe'");
?>

But this is of course the bare minimum, from here you can play with your variables, you can process return values, etc. Also watch our for SQL injection.

You will have to learn a bit of MySQL to do this. Here are a few simple examples that can help:

Retrieve information from the database:

SELECT * FROM table_name; 

https://www.w3schools.com/sql/sql_select.asp

Create a new record in the database:

INSERT INTO table_name VALUES (value1, value2, value3, ...);

https://www.w3schools.com/sql/sql_insert.asp

Update a record in the database:

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; 

https://www.w3schools.com/sql/sql_update.asp

Detele a record in the database:

DELETE FROM table_name WHERE condition;

https://www.w3schools.com/sql/sql_delete.asp

  • Related