Ok I don't understand why the update statement only works once.. and after it rolls another 6, it wont add another 500 to the RP amount.

<?php
session_start();
include("logincheck.php");
?>
<?php include_once("header.php"); ?>
<td width='100%' valign='top' align='center'>
<center><?php
$dice = rand(1,6);
echo "You rolled a<br /><b>{$dice}</b>\n";

if($dice == 6)
{
    include("haha.php");
    $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);
    $winnings = "500";
    $username = $_SESSION['username'];
    $sql = "UPDATE `Member` SET `rp` = 'rp' + '$winnings' WHERE `username` = '$username'";
    mysqli_query($cxn,$sql);
}
?></center>
</td></p>
<?php include_once("footer.php"); ?>

Can anyone figure out why? I mean it works one time...

Dani AI

Generated

As pointed out, the immediate cause was the way the rp value was being handled — that fixed the one-off symptom. Beyond that, there are three practical things to do so the game stays correct, secure, and reliable as it grows: make rp a numeric column, perform the increment with a single atomic statement, and avoid building SQL with untrusted strings.

Use a prepared statement (bind numeric types) and check for errors so failures are visible. Example pattern (prepare, bind, execute, check affected rows):

$winnings = (int)500;
$stmt = $mysqli->prepare("UPDATE Member SET rp = rp + ? WHERE username = ?");
$stmt->bind_param("is", $winnings, $username);
$stmt->execute();
if ($stmt->errno) { error_log($stmt->error); }

A single UPDATE that increments rp is atomic in the database engine; avoid the read-modify-write SELECT then UPDATE pattern unless you use transactions/SELECT ... FOR UPDATE. See the MySQL UPDATE/transaction notes for details: MySQL UPDATE.

For the “20 rolls per day” rule (asked by ), track each roll in a small history table and count rolls since midnight, or keep a per-user daily counter with a last-reset timestamp. Example check:

$stmt = $mysqli->prepare("SELECT COUNT(*) FROM roll_history WHERE user_id = ? AND rolled_at >= CURDATE()");
$stmt->bind_param("i", $uid);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();

Also validate the session username server-side, use prepared statements to prevent injection (PHP mysqli prepared statements), and follow OWASP guidance on SQL injection prevention (OWASP cheat sheet).

Recommended Answers

All 7 Replies

You don't have a loop so it makes sense that it would only execute once when $dice is equal to 6. If your intent is to keep generating random values and showing what was rolled, you need a While loop and you will need to change your code to put any one-time actions (like the mysqli) before you start the loop.

You don't have a loop so it makes sense that it would only execute once when $dice is equal to 6. If your intent is to keep generating random values and showing what was rolled, you need a While loop and you will need to change your code to put any one-time actions (like the mysqli) before you start the loop.

No I dont want them to roll until they roll a six. Its like a random game. They play 20 times a day. They roll 20 times to see if they can roll a six and every time they do, they get 500rp. The other times they just dont get anything.

And its still only generating once. Does anyone get what Im saying?? D:

The problem is that you are treating rp as a text value.

Try this:

$sql = "UPDATE `Member` SET `rp` = rp+$winnings WHERE `username` = '$username'";
Member Avatar for Member #334542

They roll 20 times to see if they can roll a six

How do you define this 20 times? your code only works on page refreshing, no loops are there. So how come its possible?

Im not sure on making it 20 times daily yet, but Im creating a database for the game and... nevermind. I already know how to make it 20 times daily.

EverWebby, you are the best!!!! ITS WORKING NOW (:

Glad to be of assistance

Make sure you clean your data and validate the username as some simple cracking will destroy your project.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.