It's working great, but at some points after one of the random events goes through, I reload the page and it either gives me or takes away some starpoints without showing the message.. but it shows the message all the other times.
Could someone help me debug this and see why it's doing that?

<?php
include("config.php");

//To change the odds, change the second number in the rand() function.
$rand = rand(1,3);
if($rand == 1)
{
	$sql = "SELECT * FROM randomevents WHERE rarity <= '10'";
	$result = mysqli_query($cxn, $sql);
	//Write the events in here with the opener: $event[] = "#your event#";
	while ($row = mysqli_fetch_assoc($result))
	{
		$event[] = $row['phrase'];
	}
	
	//This will pick a random event and show it
	$renum = rand(0,count($event));
	$display = $event[$renum];
	
	if ($display == "")
	{
		$eventdisplay = "";
	}
	else
	{
		$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\"><br>
		<tr><br>
		<td><center><b><h1>Random Event</h1></b></center></td><br>
		</tr><br>
		<tr><br>
		<td><img src=\"".$_SERVER['SCRIPT_NAME']."\">
		<p><center>".$display."</center></p><br>
		</td><br>
		</table><br>";
		
		$sql = "SELECT type FROM randomevents WHERE phrase='".$display."'";
		$result = mysqli_query($cxn, $sql);
		$row = mysqli_fetch_assoc($result);
		if ($row['type'] == "gainsp")
		{
			$rand = rand(200,500);
			$sql = "UPDATE members SET starpoints = starpoints+$rand WHERE userid='".$_SESSION['userid']."'";
			mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
		}
		elseif ($row['type'] == "losesp")
		{
			$rand = rand(50,100);
			$sql = "UPDATE members SET starpoints = starpoints-$rand WHERE userid='".$_SESSION['userid']."'";
			mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
		}
		else
		{}
	}
}
else
{
	$eventdisplay = "";
}
?>

Dani AI

Generated

The behavior described is consistent with a second HTTP request being sent to the same PHP file without rendering the page HTML. The generated event HTML in your code includes an image whose src points back to the script itself; the browser will load that src as a separate request. That extra request runs your random-event code again (and can update starpoints) but the response is fetched as an image, so no visible event message appears. This matches the “starpoints change but no message” symptom.

’s idea to use str_replace for inserting the numeric value into the phrase is fine for formatting the message, but it does not prevent the duplicate request that causes the hidden update.

Two straightforward fixes:

  • Stop using the page URL as an image source. Replace the <img> with a static image file or remove it entirely. If you need a dynamic image, point to a separate script that does not run the random-event logic.
  • Add guards so the event code only runs for the main HTML page and only once per page load. For example, skip the event when the request’s Accept header does not include text/html, and set a session flag once the event is handled:
if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'text/html') === false) {
    // Subresource (image/etc.) request — do not run event
} else {
    if (empty($_SESSION['random_event_done'])) {
        $_SESSION['random_event_done'] = time();
        // pick event and update DB (only once per page load)
    }
}

Additional hardening: use array_rand() (or rand(0, count($events)-1)) to avoid off-by-one selection, avoid using the phrase text in SQL WHERE clauses (use an ID), and use prepared statements for updates. See array_rand and the mysqli prepared statements guide. To verify the root cause during debugging, watch the Network panel in browser devtools — you should see two requests when the image src points to the same script.

Member Avatar for Member #900861

I think I have understood.

You could get the "phrase" string from database and then REPLACE { $rand } with the number of points they have earned using str_replace : http://www.php.net/manual/en/function.str-replace.php

Hope this helps ;)

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.