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=\"".$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=\"".$image."\" style=\"position:relative;float:left;\">
						<p><center>".$display."</center></p>
						</td>
						</table><br>";
					}
				}
			}
		}
	}
}

Dani AI

Generated

As showed, the claim button and the INSERT/UPDATE are buried inside the random-event branch. That means when the form is submitted the page runs the same random logic again — most of the time the branch that contains the POST handler won't execute, so nothing happens. Move the form handling out of the random-selection block (as suggested) and persist which event/item was shown (hidden fields or session) so the handler always has the item identity when POST arrives.

A practical pattern to use: handle POST at the very top of the script, then generate the random event for display. Use a hidden itemid (and optionally name/image) in the form, prepared statements for DB safety, and PRG (redirect after success) to avoid duplicate submissions. Example handler (place before any random logic):

/* at top of script */
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['claimitem'], $_POST['itemid'])) {
    $uid  = (int)($_SESSION['userid'] ?? 0);
    $iid  = (int)$_POST['itemid'];
    $name = $_POST['itemname'] ?? '';
    $img  = $_POST['itemimage'] ?? '';

    // requires UNIQUE KEY on (userid,itemid)
    $stmt = $cxn->prepare(
      "INSERT INTO useritems (userid,itemid,name,image,quantity)
       VALUES (?, ?, ?, ?, 1)
       ON DUPLICATE KEY UPDATE quantity = quantity + 1"
    );
    $stmt->bind_param('iiss', $uid, $iid, $name, $img);
    if (! $stmt->execute()) error_log('Claim error: ' . $stmt->error);
    else { header('Location: ' . $_SERVER['REQUEST_URI']); exit; }
}

Use a form that carries the item identifier:

<form method="post" action="">
  <input type="hidden" name="itemid" value="<?= htmlspecialchars($itemid) ?>">
  <input type="hidden" name="itemname" value="<?= htmlspecialchars($itemname) ?>">
  <input type="hidden" name="itemimage" value="<?= htmlspecialchars($itemimage) ?>">
  <input type="submit" name="claimitem" value="Claim Item">
</form>

Quick checklist: ensure session_start() runs, $_SESSION['userid'] exists, the handler runs before the random generator, enable error logging (error_log($stmt->error) or mysqli_error()), and consider adding a UNIQUE index on (userid,itemid) if using ON DUPLICATE KEY. This approach preserves the displayed event across requests and makes the insert/update reliable.

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.