So I'm working on a website that involves credits. I've been researching the Internet for about 30 minutes now, trying to figure a solution for updating credits. By updating credits I mean this.
The webpage loads credits from the MySQL database, the player then uses a portion of some credits (Keep in mind I have subtracted them once used), but they don't update when subtracted.
So I used a JavaScript method that sets the credits to a LI in HTML. I use PHP inside of the JavaScript code. HOWEVER, it retrieves an amount of credits, but it's not the update amount of credits. It's the previous or it stays frozen on the previous amount of credits.
So here is the code you'll need to know the most:
This supposedly 'updates' the LI using JavaScript and PHP:
function finalL(){
document.getElementById("updatecredits").innerHTML = "credits: " +
<?php
$db = mysqli_connect('localhost', 'root', 'iGenX772', 'userdata');
$query = mysqli_fetch_array(mysqli_query($db, "SELECT credits FROM users WHERE username='" . mysqli_real_escape_string($db, $_SESSION['user'] ['username']) . "'"));
echo $query['credits'];
mysqli_close($db);
?>;
}
First called when the PHP page loads:
<li><a id="updatecredits">Credits:
<?php
$db = mysqli_connect('localhost', 'root', 'iGenX772', 'userdata');
$query = mysqli_fetch_array(mysqli_query($db, "SELECT credits FROM users WHERE username='" . mysqli_real_escape_string($db, $_SESSION['user']['username']) . "'"));
echo $query['credits'];
mysqli_close($db);
?></a></li>
I activate the function through another part which is probably unnecessary for any use for this issue. BUT I am subtracting the credits BEFORE this function is called.
I use the same method in the function as I do in the List. If anyone has a solution to this issue, I would love it!
~mrgreen33gamer | Joshua
EXTRA:
<div id="logreg">
<ul>
<li><a>Welcome, <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?></a></li>
<li><a><span id="updatecredits">Credits:
<?php
$db = mysqli_connect('localhost', 'root', 'iGenX772', 'userdata');
$query = mysqli_fetch_array(mysqli_query($db, "SELECT credits FROM users WHERE username='" . mysqli_real_escape_string($db, $_SESSION['user']['username']) . "'"));
echo $query['credits'];
mysqli_close($db);
?></span></a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>