Jump to content

Not Inserting OR Updating?


Tenaciousmug

Recommended Posts

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>";
				}
			}
		}
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/236021-not-inserting-or-updating/
Share on other sites

Your code does not work as expected because your logic is flawed from the start. Such as why the need for this:

$rand = rand(1,3);
if ($rand == 1)
{
$rand = rand(1,3);
if($rand == 1)
{

That makes no logical sense at all. The likelyhood of rand(1,3) returning the number 1 twice on every page load is very unlikely. So when you first go to your page it may return the number 1 twice and show your form for claiming the prize. But when you submit your form it'll reload your page and this time rand(1,3) will return a different number other than one and so your code will never run. I suggest that you remove those two if's.

 

You can select a random row using MySQL. Simply add the following code ORDER BY rand() LIMIT 1 to the end of your query.

		$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];

So with that addition your code then just becomes

		$sql = "SELECT phrase FROM randomevents WHERE rarity = '1' ORDER BY rand() LIMIT 1";
	$result = mysqli_query($cxn, $sql);
	$row = mysqli_fetch_assoc($result)
	$display =  $row['phrase'];

 

This code

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>";
				}

Should be at the top of your file

 

Also in order for the code to to either update/insert the random item in your database you need to pass the $itemid from your form. Eg

			$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=\"hidden\" name=\"itemid\" value=\"$itemid\" />
				<input type=\"submit\" name=\"claimitem\" value=\"Claim Egg!\" />
				</form>
				</p>
				</td>
				</table><br>";

Now you'll get the itemid from your form using $_POST['itemid'].

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.