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 = "";
}
?>
Member Avatar for cuonic

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.