Jump to content

Array Help!!


katarra

Recommended Posts

Okay, I'm trying to create a script where, when you purchase something, it updates the "quantity" field +1 within the "inventory" table.

 

THANKS!

 

Here's the errors I'm getting:

Notice: Undefined property: stdClass::$quantity in /home/katarra/public_html/shop.php on line 40

Input Array does not match ?: update `inventory` set `quantity`=1 where `player`=

 

And here's the script:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include("lib.php");
define("PAGENAME", "Shop");
$player = check_user($secret_key, $db);

switch($_GET['act'])
{
case "buy":
	if (!$_GET['id']) //No item ID
	{
		header("Location: shop.php");
		break;
	}

	//Select the item from the database
	$query = $db->execute("select `id`, `name`, `price` from `items` where `id`=?", array($_GET['id']));

	//Invalid item (it doesn't exist)
	if ($query->recordcount() == 0)
	{
		header("Location: shop.php");
		break;
	}

	$item = $query->fetchrow();
	if ($item['price'] > $player->gold)
	{
		echo "<b>Shop Keeper:</b><br />\n";
		echo "<i>Sorry, but you cannot afford this!</i><br /><br />\n";
		echo "<a href=\"inventory.php\">Return to inventory</a> | <a href=\"shop.php\">Return to shop</a>";
		break;
	}

	$query1 = $db->execute("update `users` set `gold`=? where `id`=?", array($player->gold - $item['price'], $player->id));
	$insert['player'] = $player->id;
	$insert['item_id'] = $item['id'];
	$query = $db->execute("update `inventory` set `quantity`=? where `player`=?", array($player->quantity + 1));
	$query2 = $db->autoexecute('inventory', $insert, 'INSERT');
	if ($query1 && $query2) //If successful
	{
		$player = check_user($secret_key, $db); //Get new user stats

		echo "<b>Shop Keeper:</b><br />\n";
		echo "<i>Thank you, enjoy your new <b>" . $item['name'] . "</b>!</i><br /><br />\n";
		echo "<a href=\"inventory.php\">Return to inventory</a> | <a href=\"shop.php\">Return to shop</a>";
		break;
	}
	else
	{
		//Error logging here
	}

	break;

case "sell":
	if (!$_GET['id']) //No item ID
	{
		header("Location: shop.php");
		break;
	}

	//Select the item from the database
	$query = $db->execute("select inventory.id, items.name, items.price from `items`, `inventory` where inventory.item_id=items.id and inventory.player_id=? and inventory.id=?", array($player->id, $_GET['id']));

	//Either item doesn't exist, or item doesn't belong to user
	if ($query->recordcount() == 0)
	{
		echo "Sorry, that item does not exist!";
		break;
	}

	$sell = $query->fetchrow(); //Get item info

	//Check to make sure clicking Sell wasn't an accident
	if (!$_POST['sure'])
	{
		echo "Are you sure you want to sell your <b>" . $sell['name'] . "</b> for <b>" . floor($sell['price']/2) . "</b> gold?<br /><br />\n";
		echo "<form method=\"post\" action=\"shop.php?act=sell&id=" . $sell['id'] . "\">\n";
		echo "<input type=\"submit\" name=\"sure\" value=\"Yes, I am sure!\" />\n";
		echo "</form>\n";
		break;
	}

	//Delete item from database, add gold to player's account
	$query = $db->execute("delete from `inventory` where `id`=?", array($sell['id']));
	$query = $db->execute("update `users` set `gold`=? where `id`=?", array($player->gold + floor($sell['price']/2), $player->id));

	$player = check_user($secret_key, $db); //Get updated user info

	echo "You have sold your <b>" . $sell['name'] . "</b> for <b>" . floor($sell['price']/2) . "</b> gold.<br /><br />\n";
	echo "<a href=\"inventory.php\">Return to inventory</a> | <a href=\"shop.php\">Return to shop</a>";
	break;

case "weapon":
	//Check in case somebody entered 0
	$_GET['fromprice'] = ($_GET['fromprice'] == 0)?"":$_GET['fromprice'];
	$_GET['toprice'] = ($_GET['toprice'] == 0)?"":$_GET['toprice'];
	$_GET['fromeffect'] = ($_GET['fromeffect'] == 0)?"":$_GET['fromeffect'];
	$_GET['toeffect'] = ($_GET['toeffect'] == 0)?"":$_GET['toeffect'];

	//Construct query
	$query = "select `id`, `name`, `slot`, `price`, `type`, `bonus`, `target`, `attr`, `effect`, `value` from `items` where ";
	$query .= ($_GET['name'] != "")?"`name` LIKE  ? and ":"";
	$query .= ($_GET['fromprice'] != "")?"`price` >= ? and ":"";
	$query .= ($_GET['toprice'] != "")?"`price` <= ? and ":"";
	$query .= ($_GET['fromeffect'] != "")?"`value` >= ? and ":"";
	$query .= ($_GET['toeffect'] != "")?"`value` <= ? and ":"";

	$query .= "`type`='weapon' order by `price` asc";

	//Construct values array for adoDB
	$values = array();
	if ($_GET['name'] != "")
	{
		array_push($values, "%".trim($_GET['name'])."%");
	}
	if ($_GET['fromprice'])
	{
		array_push($values, intval($_GET['fromprice']));
	}
	if ($_GET['toprice'])
	{
		array_push($values, intval($_GET['toprice']));
	}
	if ($_GET['fromeffect'])
	{
		array_push($values, intval($_GET['fromeffect']));
	}
	if ($_GET['toeffect'])
	{
		array_push($values, intval($_GET['toeffect']));
	}

	$query = $db->execute($query, $values); //Search!

	echo "<fieldset>";
	echo "<legend><b>Shop Keeper:</b></legend>\n";
	echo "<i>What would you like to see, sir?</i><br /><br />\n";
	echo "<form method=\"get\" action=\"shop.php\">\n";
	echo "<table width=\"100%\">\n";
	echo "<tr>\n<td width=\"40%\">Name:</td>\n";
	echo "<td width=\"60%\"><input type=\"text\" name=\"name\" value=\"" . stripslashes($_GET['name']) . "\" /></td>\n";
	echo "</td>\n</tr>";
	echo "<tr>\n<td width=\"40%\">Price:</td>\n";
	echo "<td width=\"60%\"><input type=\"text\" name=\"fromprice\" size=\"4\" value=\"" . stripslashes($_GET['fromprice']) . "\" /> to <input type=\"text\" name=\"toprice\" size=\"4\" value=\"" . stripslashes($_GET['toprice']) . "\" /></td>\n";
	echo "</td>\n</tr>";
	echo "<tr>\n<td width=\"40%\">Type:</td>\n";
	echo "<td width=\"60%\"><select name=\"act\" size=\"2\">\n";
	echo "<option value=\"weapon\" selected=\"selected\">Weapons</option>\n";
	echo "<option value=\"shield\">Shields</option>\n";
	echo "</select></td>\n</tr>\n";
	echo "<tr>\n<td></td>";
	echo "<td><input type=\"submit\" value=\"Submit\" /></td>\n</tr>";
	echo "</table>";
	echo "</form>\n";
	echo "</fieldset>";
	echo "<br /><br />";
	echo "<b>Shop Keeper:</b><br />\n";
	echo "<i>Here's our collection:</i><br /><br />\n";

	if ($query->recordcount() == 0)
	{
		echo "No items found! Try changing your search criteria.";
	}
	else
	{
		while ($item = $query->fetchrow())
		{
			echo "<fieldset>\n";
			echo "<legend><b>" . $item['name'] . "</b></legend>\n";
			echo "<table width=\"100%\">\n";
			echo "<tr><td width=\"85%\">";
			echo "</td><td width=\"15%\">";
			echo "<b>Price:</b> " . $item['price'] . "<br />";
			echo "<a href=\"shop.php?act=buy&id=" . $item['id'] . "\">Buy</a><br />";
			echo "</td></tr>\n";
			echo "</table>";
			echo "</fieldset>\n<br />";
		}
	}
	break;

case "shield":
	//Check in case somebody entered 0
	$_GET['fromprice'] = ($_GET['fromprice'] == 0)?"":$_GET['fromprice'];
	$_GET['toprice'] = ($_GET['toprice'] == 0)?"":$_GET['toprice'];
	$_GET['fromeffect'] = ($_GET['fromeffect'] == 0)?"":$_GET['fromeffect'];
	$_GET['toeffect'] = ($_GET['toeffect'] == 0)?"":$_GET['toeffect'];

	//Construct query
	$query = "select `id`, `name`, `slot`, `price`, `type`, `bonus`, `target`, `attr`, `effect`, `value` from `items` where ";
	$query .= ($_GET['name'] != "")?"`name` LIKE  ? and ":"";
	$query .= ($_GET['fromprice'] != "")?"`price` >= ? and ":"";
	$query .= ($_GET['toprice'] != "")?"`price` <= ? and ":"";
	$query .= ($_GET['fromeffect'] != "")?"`value` >= ? and ":"";
	$query .= ($_GET['toeffect'] != "")?"`value` <= ? and ":"";

	$query .= "`type`='shield' order by `price` asc";

	//Construct values array for adoDB
	$values = array();
	if ($_GET['name'] != "")
	{
		array_push($values, "%".trim($_GET['name'])."%");
	}
	if ($_GET['fromprice'])
	{
		array_push($values, intval($_GET['fromprice']));
	}
	if ($_GET['toprice'])
	{
		array_push($values, intval($_GET['toprice']));
	}
	if ($_GET['fromeffect'])
	{
		array_push($values, intval($_GET['fromeffect']));
	}
	if ($_GET['toeffect'])
	{
		array_push($values, intval($_GET['toeffect']));
	}

	$query = $db->execute($query, $values); //Search!

	echo "<fieldset>";
	echo "<legend><b>Shop Keeper:</b></legend>\n";
	echo "<i>What would you like to see, sir?</i><br /><br />\n";
	echo "<form method=\"get\" action=\"shop.php\">\n";
	echo "<table width=\"100%\">\n";
	echo "<tr>\n<td width=\"40%\">Name:</td>\n";
	echo "<td width=\"60%\"><input type=\"text\" name=\"name\" value=\"" . stripslashes($_GET['name']) . "\" /></td>\n";
	echo "</td>\n</tr>";
	echo "<tr>\n<td width=\"40%\">Price:</td>\n";
	echo "<td width=\"60%\"><input type=\"text\" name=\"fromprice\" size=\"4\" value=\"" . stripslashes($_GET['fromprice']) . "\" /> to <input type=\"text\" name=\"toprice\" size=\"4\" value=\"" . stripslashes($_GET['toprice']) . "\" /></td>\n";
	echo "</td>\n</tr>";
	echo "<tr>\n<td width=\"40%\">Type:</td>\n";
	echo "<td width=\"60%\"><select name=\"act\" size=\"2\">\n";
	echo "<option value=\"weapon\">Weapons</option>\n";
	echo "<option value=\"shield\" selected=\"selected\">Shields</option>\n";
	echo "</select></td>\n</tr>\n";
	echo "<tr>\n<td></td>";
	echo "<td><input type=\"submit\" value=\"Submit\" /></td>\n</tr>";
	echo "</table>";
	echo "</form>\n";
	echo "</fieldset>";
	echo "<br /><br />";
	echo "<b>Shop Keeper:</b><br />\n";
	echo "<i>Here's our collection:</i><br /><br />\n";

	if ($query->recordcount() == 0)
	{
		echo "No items found! Try changing your search criteria.";
	}
	else
	{
		while ($item = $query->fetchrow())
		{
			echo "<fieldset>\n";
			echo "<legend><b>" . $item['name'] . "</b></legend>\n";
			echo "<table width=\"100%\">\n";
			echo "<tr><td width=\"85%\">";
			echo "</td><td width=\"15%\">";
			echo "<b>Price:</b> " . $item['price'] . "<br />";
			echo "<a href=\"shop.php?act=buy&id=" . $item['id'] . "\">Buy</a><br />";
			echo "</td></tr>\n";
			echo "</table>";
			echo "</fieldset>\n<br />";
		}
	}

	break;

default:
	//Show search form
	echo "<fieldset>";
	echo "<legend><b>Shop Keeper:</b></legend>\n";
	echo "<i>What would you like to see, sir?</i><br /><br />\n";
	echo "<form method=\"get\" action=\"shop.php\">\n";
	echo "<table width=\"100%\">\n";
	echo "<tr>\n<td width=\"40%\">Name:</td>\n";
	echo "<td width=\"60%\"><input type=\"text\" name=\"name\" /></td>\n";
	echo "</td>\n</tr>";
	echo "<tr>\n<td width=\"40%\">Price:</td>\n";
	echo "<td width=\"60%\"><input type=\"text\" name=\"fromprice\" size=\"4\" /> to <input type=\"text\" name=\"toprice\" size=\"4\" /></td>\n";
	echo "</td>\n</tr>";
	echo "<tr>\n<td width=\"40%\">Type:</td>\n";
	echo "<td width=\"60%\"><select name=\"act\" size=\"2\">\n";
	echo "<option value=\"weapon\" selected=\"selected\">Weapons</option>\n";
	echo "<option value=\"shield\">Shields</option>\n";
	echo "</select></td>\n</tr>\n";
	echo "<tr>\n<td></td>";
	echo "<td><input type=\"submit\" value=\"Submit\" /></td>\n</tr>";
	echo "</table>";
	echo "</form>\n";
	echo "</fieldset>";
	break;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/195112-array-help/
Share on other sites

From query1, to break. I changed it completely. I've saved it, and it now works. I have deleted your purchase and someone elses with 0. Have a look. It's a bit messy, but it works absolutely perfect!

 

      $query1 = $db->execute("update `users` set `gold`=? where `id`=?", array($player->gold - $item['price'], $player->id));
      $insert['player'] = $player->id;
      $insert['item_id'] = $item['id'];
      
      $query2 = $db->autoexecute('inventory', $insert, 'INSERT');
      
      $q = $db->execute("select * from `inventory` where `player`='".$insert['player']."'");
      $row = $q->fetchrow();
      
      $query = $db->execute("update `inventory` set `quantity`=? where `player`='".$insert['player']."'", array($row['quantity'] + 1));

      if ($query && $query1) //If successful
      {
         $player = check_user($secret_key, $db); //Get new user stats
         
         echo "<b>Shop Keeper:</b><br />\n";
         
         if ($query2){echo "<strong>You purchased a new item!</strong><br />";}
         
         echo "<i>Thank you, enjoy your new <b>" . $item['name'] . "</b>!</i><br /><br />\n";

         $q1 = $db->execute("select * from `inventory` where `player`='".$insert['player']."'");
         $row1 = $q1->fetchrow();

         echo "<i>You now have " . $row1['quantity'] . " <b>" . $item['name'] . "(s)</b>!</i><br /><br />\n";
         echo "<a href=\"inventory.php\">Return to inventory</a> | <a href=\"shop.php\">Return to shop</a>";
         break;
      }

 

The reason why it didn't work, is because $player doesn't define inventory, only users. So.. I had to define the query in this code to allow quantities :)

Link to comment
https://forums.phpfreaks.com/topic/195112-array-help/#findComment-1025641
Share on other sites

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.