Jump to content

$_GET Help


Stalingradd

Recommended Posts

Hello. =] I've been having trouble understanding $_GET, (in PHP); when there's something like this: ?action=buy&itemid=1. I programmed a Shopping Cart for the website that I'm making, and when somebody clicks on the item that they'd like to purchase; (the link is: shops.php?action=buy&itemid=$itemid); the page comes up blank, and nothing happends. I have no clue what's wrong with it, and I'm becomming VERY frustrated. Does anybody know where I "messed up"? Here's the entire Shopping Cart code:

 

<?php

session_start();

include("config.php");

if($_SESSION['username'] ==  "" || $_SESSION['password'] == "") {

notloggedin();

}

if($_SESSION['username'] != "" && $_SESSION['password'] != "") {

start();

$action = $_GET['action'];

$shopid = $_GET['shopid'];

 

if($action != "buy") {

$show_query = mysql_query("SELECT * FROM items WHERE shopid ='$shopid' ORDER BY itemid");

for($i=0;$i<@mysql_numrows($show_query);$i++) { 

$itemid=mysql_result($show_query,$i,"itemid");

$showname=mysql_result($show_query,$i,"name"); 

$showimage=mysql_result($show_query,$i,"image");

$showprice=mysql_result($show_query,$i,"price");

$showamount=mysql_result($show_query,$i,"amount");

$showdescription=mysql_result($show_query,$i,"description");

$showrarity=mysql_result($show_query,$i,"rarity"); 

if($i%4==0) echo "<tr>";

echo "<td><center>$showname<br><a href=shops.php?action=buy><img src=../images/items/$showimage border=\"0\"></a><br>$showprice EDC<br>Amount: $showamount</center></td>"; 

if($i%4==2) echo "</tr>";

}

if($action == "buy") {

$items_id = $_GET['itemid'];

$the_item = mysql_query("SELECT * FROM items WHERE itemid='$items_id'");

while($showit = mysql_fetch_array($the_item)) {

$item__id = $showit['id'];

$item__name = $showit['name'];

$item__image = $showit['image'];

$item__price = $showit['price'];

$item__amount = $showit['amount'];

}

$checkamount = "$credits - $item__price";

if($checkprice < "0") {

echo "<font size=6>Shops</font><br><br>Error!<br><br>";

$show_query = mysql_query("SELECT * FROM items WHERE shopid ='$shopid' ORDER BY itemid");

for($i=0;$i<@mysql_numrows($show_query);$i++) { 

$itemid=mysql_result($show_query,$i,"itemid");

$showname=mysql_result($show_query,$i,"name"); 

$showimage=mysql_result($show_query,$i,"image");

$showprice=mysql_result($show_query,$i,"price");

$showamount=mysql_result($show_query,$i,"amount");

$showdescription=mysql_result($show_query,$i,"description");

$showrarity=mysql_result($show_query,$i,"rarity"); 

if($i%4==0) echo "<tr>";

echo "<td><center>$showname<br><a href=shops.php&action=buy&itemid=$itemid><img src=../images/items/$showimage border=\"0\"></a><br>$showprice EDC<br>Amount: $showamount</center></td>"; 

if($i%4==2) echo "</tr>";

}

if($checkprice >= 0) {

$itemsid = $_GET['itemsid'];

$theitem = mysql_query("SELECT * FROM items WHERE itemid='$itemsid'");

while($show = mysql_fetch_array($theitem)) {

$item_id = $show['itemid'];

$item_name = $show['name'];

$item_image = $show['image'];

$item_price = $show['price'];

$item_amount = $show['amount'];

mysql_query("UPDATE items SET amount = '$item_amount' - 1 WHERE itemid='$item_id'");

mysql_query("INSERT INTO useritems (usename, item, location) VALUES ('$username', '$item_name', 'Inventory')");

mysql_query("UPDATE users SET credits='$credits' - '$item_price' WHERE username='$username'");

}

 

echo "<font size=6>Shops</font><br><br>Success! You have bought that Item for $item_price USD.<br><img src=images/items/$item_image><br><br><< <a href=shops.php?shopid=1><< Back to Shop</a> | <a href=map.php>Back to Map >></a>";

}

}

}

}

}

?>

 

 

Thank You VERY Much! =]

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

Here, I cleaned up the code also:

 

<?php
session_start();
include("config.php");
if($_SESSION['username'] ==  "" || $_SESSION['password'] == "") {
notloggedin();
}
if($_SESSION['username'] != "" && $_SESSION['password'] != "") {
start();
$action = $_GET['action'];
$shopid = $_GET['shopid'];

if($action != "buy") {
	$show_query = mysql_query("SELECT * FROM items WHERE shopid ='$shopid' ORDER BY itemid");
	for($i=0;$i<@mysql_numrows($show_query);$i++) {   
		$itemid=mysql_result($show_query,$i,"itemid");
		$showname=mysql_result($show_query,$i,"name"); 
		$showimage=mysql_result($show_query,$i,"image");
		$showprice=mysql_result($show_query,$i,"price");
		$showamount=mysql_result($show_query,$i,"amount");
		$showdescription=mysql_result($show_query,$i,"description");
		$showrarity=mysql_result($show_query,$i,"rarity"); 
		if($i%4==0) echo "<tr>";
		echo "<td><center>$showname
		<a href=shops.php?action=buy><img src=../images/items/$showimage border=\"0\">[/url]
		$showprice EDC
		Amount: $showamount</center></td>"; 
		if($i%4==2) echo "</tr>";
	}
} elseif ($action == "buy") { // HERE WAS THE PROBLEM. THE IF STATEMENT WAS IN THE !BUY IF. SO THIS WOULD NOT RUN.
	$items_id = $_GET['itemid'];
	$the_item = mysql_query("SELECT * FROM items WHERE itemid='$items_id'");
	while($showit = mysql_fetch_array($the_item)) {
		$item__id = $showit['id'];
		$item__name = $showit['name'];
		$item__image = $showit['image'];
		$item__price = $showit['price'];
		$item__amount = $showit['amount'];
	}
	$checkamount = "$credits - $item__price";
	if($checkprice < "0") {
		echo "<font size=6>Shops</font>

		Error!

		";
		$show_query = mysql_query("SELECT * FROM items WHERE shopid ='$shopid' ORDER BY itemid");
		for($i=0;$i<@mysql_numrows($show_query);$i++) {   
			$itemid=mysql_result($show_query,$i,"itemid");
			$showname=mysql_result($show_query,$i,"name"); 
			$showimage=mysql_result($show_query,$i,"image");
			$showprice=mysql_result($show_query,$i,"price");
			$showamount=mysql_result($show_query,$i,"amount");
			$showdescription=mysql_result($show_query,$i,"description");
			$showrarity=mysql_result($show_query,$i,"rarity"); 
			if($i%4==0) echo "<tr>";
			echo "<td><center>$showname
			<a href=shops.php&action=buy&itemid=$itemid><img src=../images/items/$showimage border=\"0\">[/url]
			$showprice EDC
			Amount: $showamount</center></td>"; 
			if($i%4==2) echo "</tr>";
		}
		if($checkprice >= 0) {
			$itemsid = $_GET['itemsid'];
			$theitem = mysql_query("SELECT * FROM items WHERE itemid='$itemsid'");
			while($show = mysql_fetch_array($theitem)) {
				$item_id = $show['itemid'];
				$item_name = $show['name'];
				$item_image = $show['image'];
				$item_price = $show['price'];
				$item_amount = $show['amount'];
				mysql_query("UPDATE items SET amount = '$item_amount' - 1 WHERE itemid='$item_id'");
				mysql_query("INSERT INTO useritems (usename, item, location) VALUES ('$username', '$item_name', 'Inventory')");
				mysql_query("UPDATE users SET credits='$credits' - '$item_price' WHERE username='$username'");
			}

			echo "<font size=6>Shops</font>

			Success! You have bought that Item for $item_price USD.
			<img src=images/items/$item_image>

			<< <a href=shops.php?shopid=1><< Back to Shop[/url] | <a href=map.php>Back to Map >>[/url]";
		}
	}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/101089-_get-help/#findComment-516991
Share on other sites

Wow, thank you so much! I still have a problem, though. The database doesn't update anything, and the page either displays what would happen if I didn't have enough credits, (and I do); and if Itry to put an elseif in fron of ($checkprice < "0"), I get the unexpeted ELSEIF error or something like that.

Link to comment
https://forums.phpfreaks.com/topic/101089-_get-help/#findComment-517042
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.