This code will not insert OR update. But on the other ones it will.. but for this one, I'm doing an extra step to say that they can claim the item or not. From adding that extra step, it won't insert or update.

$rand = rand(1,3);
if ($rand == 1)
{
	$rand = rand(1,3);
	if($rand == 1)
	{
		$sql = "SELECT * FROM randomevents WHERE rarity = '1'";
		$result = mysqli_query($cxn, $sql);
		while ($row = mysqli_fetch_assoc($result))
		{
			$event[] = $row['phrase'];
		}
		
		//This will pick a random event and show it
		$renum = rand(0,count($event)-1);
		$display = $event[$renum];
		
		if ($display == "")
		{
			$eventdisplay = "";
		}
		else
		{
			$sql = "SELECT * FROM randomevents WHERE phrase='".mysqli_real_escape_string($cxn,$display)."'";
			$result = mysqli_query($cxn, $sql) or die("Query died: select everything from randomevent table");
			$row = mysqli_fetch_array($result);
			
			$itemid = $row['itemid'];
			$image = $row['image'];
			
			if ($row['type'] == "itemgain")
			{
				$sql2 = "SELECT name, image FROM items WHERE itemid='".$itemid."'";
				$result2 = mysqli_query($cxn, $sql2) or die(mysqli_error($cxn));
				$row2 = mysqli_fetch_assoc($result2);
				
				$itemname = $row2['name'];
				$itemimage = $row2['image'];
				
				$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\">
					<tr>
					<td width=\"350px\"><center><b><h1>Random Event</h1></b></center></td>
					</tr>
					<tr>
					<td><img src=\"http://www.elvonica.com/".$image."\" style=\"position:relative;float:left;\">
					<p><center>".$display."</center></p>
					<p>
					<form action=\"".$_SERVER['SCRIPT_NAME']."\" method=\"post\">
					<input type=\"submit\" name=\"claimitem\" value=\"Claim Egg!\" />
					</form>
					</p>
					</td>
					</table><br>";
				
				if (isset($_POST['claimitem']))
				{
					$sql = "SELECT * FROM useritems WHERE itemid='".$itemid."' AND userid='".$_SESSION['userid']."'";
					$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
					$num = mysqli_num_rows($result);
					if ($num == 0)
					{
						$sql = "INSERT INTO useritems (userid, itemid, name, image, quantity) VALUES ('".$_SESSION['userid']."', '".$itemid."', '".$itemname."', '".$itemimage."', '1')";
						mysqli_query($cxn, $sql) or die("Query died: insert for itemgain");
					}
					else
					{
						$sql = "UPDATE useritems SET quantity = quantity+1 WHERE itemid='".$itemid."' AND userid='".$_SESSION['userid']."'";
						mysqli_query($cxn, $sql) or die("Query died: update for itemgain");
						$eventdisplay = "<table cellspacing=\"0\" class=\"events\" align=\"center\">
						<tr>
						<td width=\"350px\"><center><b><h1>Random Event</h1></b></center></td>
						</tr>
						<tr>
						<td><img src=\"http://www.elvonica.com/".$image."\" style=\"position:relative;float:left;\">
						<p><center>".$display."</center></p>
						</td>
						</table><br>";
					}
				}
			}
		}
	}
}

perhaps if you move your if (isset post[claimitem]) outside all of your conditianal if statements. put it at the top of your page so it is eval'd first, if a posted form value comes from that page.

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.