Jump to content

Cannot redeclare error...


zelig

Recommended Posts

I'm not seeing what is causing this: (Fatal error: Cannot redeclare shopdisplay() (previously declared in /home/katarra/public_html/shops.php:11)in /home/katarra/public_html/shops.php on line 21) error...

 

Any help would be appreciated! (This probably isn't the only error, but it's the one that's stopping me from finding the others)

 

Thanks!

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require_once('dk_lib.php');
require_once('scripts/dbfunc.s');
include ("debug.php");
include("lib.php");
$player = check_user($secret_key, $db);

function shopdisplay($content, $title) { // Finalize page and output to browser.
    
    $template = gettemplate("shops");

    $finalarray = array(
        "title"=>$title,
        "content"=>$content);
    $page = parsetemplate($template, $finalarray);
    echo $page;
    die();    
}

if (isset($_GET["do"])) {
    $do = explode(":",$_GET["do"]);
    
if ($do[0] == "buy") { include('shops.php'); buy(); }
    elseif ($do[0] == "buy2") { include('shops.php'); buy2($do[1]); }
    elseif ($do[0] == "buy3") { include('shops.php'); buy3($do[1]); }
    elseif ($do[0] == "sell") { include('shops.php'); sell(); }
} else { donothing(); }

function donothing() {
    
    $page = "Welcome to my shop. What do you want?";
    shopdisplay($page, "Shop Home");
    
}

function buy() { // Displays a list of available items for purchase.
    
    global $numqueries;
    
$query1 = doquery("SELECT * FROM levels", "levels") or trigger_error("Query failed: ". mysql_error()); 
$location = mysql_fetch_array($query1);
    $townquery = doquery("SELECT name,itemslist FROM shop WHERE location=$location", "shop") or trigger_error("Query failed: ". mysql_error()); 
    if (mysql_num_rows($townquery) != 1) { shopdisplay("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
    $townrow = mysql_fetch_array($townquery);
    
    $itemslist = explode(",",$townrow["itemslist"]);
    $querystring = "";
    foreach($itemslist as $a=>$b) {
        $querystring .= "id='$b' OR ";
    }
    $querystring = rtrim($querystring, " OR ");
    
    $itemsquery = doquery("SELECT * FROM items WHERE $querystring ORDER BY id", "items");
    $page = "Buying weapons will increase your Attack Power. Buying armor and shields will increase your Defense Power.<br /><br />Click an item name to purchase it.<br /><br />The following items are available at this shop:<br /><br />\n";
    $page .= "<table width=\"80%\">\n";
    while ($itemsrow = mysql_fetch_array($itemsquery)) {
        if ($itemsrow["type"] == 'weapon') { $attr = "Attack Power:"; } else  { $attr = "Defense Power:"; }
        $page .= "<tr><td width=\"4%\">";
/*         if  ($userrow["weaponid"] == $itemsrow["id"] || $userrow["armorid"] == $itemsrow["id"] || $userrow["shieldid"] == $itemsrow["id"]) {
            $page .= "<td width=\"32%\"><span class=\"light\">".$itemsrow["name"]."</span></td><td width=\"32%\"><span class=\"light\">$attrib ".$itemsrow["attribute"]."</span></td><td width=\"32%\"><span class=\"light\">Already purchased</span></td></tr>\n";
        } else } */
            $page .= "<td width=\"32%\"><b><a href=\"shops.php?do=buy2:".$itemsrow["id"]."\">".$itemsrow["name"]."</a>$</b></td><td width=\"32%\">$attr <b>".$itemsrow["attr"]."</b></td><td width=\"32%\">Price: <b>".$itemsrow["price"]." gold</b></td></tr>\n";
        
    }
    $page .= "</table><br />\n";
    $title = "Buy Items";
    
    shopdisplay($page, $title);
    
}

function buy2($id) { // Confirm user's intent to purchase item.
    
    global $userrow, $numqueries;
    
$query1 = doquery("SELECT * FROM levels", "levels");
$location = mysql_fetch_array($query1);
    $townquery = doquery("SELECT name,itemslist FROM shop WHERE location=$location", "shop") or trigger_error("Query failed: ". mysql_error()); 
    if (mysql_num_rows($townquery) != 1) { shopdisplay("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
    $townrow = mysql_fetch_array($townquery);
    $townitems = explode(",",$townrow["itemslist"]);
    if (! in_array($id, $townitems)) { shopdisplay("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
    
    $itemsquery = doquery("SELECT * FROM items WHERE id='$id'", "items") or trigger_error("Query failed: ". mysql_error()); 
    $itemsrow = mysql_fetch_array($itemsquery);
    
    if ($player["gold"] < $itemsrow["price"]) { shopdisplay("You do not have enough gold to buy this item!", "Buy Items"); die(); }
    
    if ($itemsrow["type"] == 'weapon') {
            $page = "You are buying the ".$itemsrow["name"].", is that ok?<br /><br /><form action=\"shops.php?do=buy3:$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
    } elseif ($itemsrow["type"] == 'armor') {
            $page = "You are buying the ".$itemsrow["name"].", is that ok?<br /><br /><form action=\"shops.php?do=buy3:$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
    } elseif ($itemsrow["type"] == 'item') {
            $page = "You are buying the ".$itemsrow["name"].", is that ok?<br /><br /><form action=\"shops.php?do=buy3:$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
    } elseif ($itemsrow["type"] == 'pet') {
            $page = "You are buying the ".$itemsrow["name"].", is that ok?<br /><br /><form action=\"shops.php?do=buy3:$id\" method=\"post\"><input type=\"submit\" name=\"submit\" value=\"Yes\" /> <input type=\"submit\" name=\"cancel\" value=\"No\" /></form>";
    }
    $title = "Buy Items";
    shopdisplay($page, $title);  
}

function buy3($id) { // Update user profile with new item & stats.
    
    if (isset($_POST["cancel"])) { header("Location: shops.php"); die(); }
    
    global $userrow;
    
$query1 = doquery("SELECT * FROM levels", "levels");
$location = mysql_fetch_array($query1);
    $townquery = doquery("SELECT name,itemslist FROM shop WHERE location=$location", "shop") or trigger_error("Query failed: ". mysql_error()); 
    if (mysql_num_rows($townquery) != 1) { shopdisplay("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
    $townrow = mysql_fetch_array($townquery);
    $townitems = explode(",",$townrow["itemslist"]);
    if (! in_array($id, $townitems)) { shopdisplay("Cheat attempt detected.<br /><br />Get a life, loser.", "Error"); }
    
    $itemsquery = doquery("SELECT * FROM items WHERE id='$id'", "items") or trigger_error("Query failed: ". mysql_error()); 
    $itemsrow = mysql_fetch_array($itemsquery);
    
    if ($player["gold"] < $itemsrow["price"]) { shopdisplay("You do not have enough gold to buy this item!", "Buy Items"); die(); }
    
	$query = mysql_select("select `id`, `name`, `price`, `type`, `slot` from `items` where `id`=?", array($_GET['id'])) or trigger_error("Query failed: ". mysql_error()); 
	$item = $query->fetchrow();

	$q1 = mysql_select("select * from `inventory` where `player`=? and item_id=?", array($player->id,$item['id'])) or trigger_error("Query failed: ". mysql_error()); 
	if($q1->recordcount() == 0)
	{
	$insert['player'] = $player->id;
	$insert['quantity'] = '0';
	$insert['status'] = 'unequipped';
	$insert['item_id'] = $item['id'];
	$insert['type'] = $item['type'];
	$insert['slot'] = $item['slot'];

	$query2 = $db->autoexecute('inventory', $insert, 'INSERT') or trigger_error("Query failed: ". mysql_error()); 
	}

	$query1 = mysql_select("update `users` set `gold`=? where `id`=?", array($player->gold - $item['price'], $player->id)) or trigger_error("Query failed: ". mysql_error()); 
	$insert['player'] = $player->id;
	$insert['item_id'] = $item['id'];


	$q = mysql_select("select * from `inventory` where `player`='".$insert['player']."' and `item_id`='".$item['id']."'") or trigger_error("Query failed: ". mysql_error()); 
	$row = $q->fetchrow();

	$query = mysql_select("update `inventory` set `quantity`=? where `item_id`='".$item['id']."' and `player`='".$insert['player']."'", array($row['quantity'] + 1)) or trigger_error("Query failed: ". mysql_error()); 

		if ($query && $query1) //If successful
	{
		$player = check_user($secret_key, $db); //Get new user stats

		echo "<b>Blacksmith:</b><br />\n";

		if ($query2){echo "<strong>You purchased a " . ucwords($item['name']) . "!</strong><br />";}

		echo "<i>Thank you, enjoy your new <b>" . ucwords($item['name']) . "</b>!</i><br /><br />\n";

		$q1 = mysql_select("select * from `inventory` where `item_id`='".$_GET['id']."' and `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=\"main.php?username=$x&password=$y&doit=inventory\">Return to inventory</a>";
		break;
	}
	else
	{
		//Error logging here
	}


}

function sell($id) {
	if (!$_GET['id']) //No item ID
	{
		echo "That item does not exist in your inventory!";
		break;
	}

	//Select the item from the database
	$inventitemid = $_GET['id'];
	$query = mysql_select("select * from `inventory` where inventory.item_id='".$inventitemid."' and inventory.player=?", array($player->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;}
	$query = mysql_select("select * from `items` where items.id=?", array($_GET['id']));
$query12 = $db->execute("SELECT `status`, `quantity` FROM `inventory` where inventory.item_id='".$inventitemid."' and inventory.player=?", array($player->id));
$row = $query12->fetchrow();
if($row['status'] == 'equipped')
{ echo "Unequip this item before trying to sell it!<br>";
echo "<a href=\"main.php?username=$x&password=$y&doit=inventory\">Return to inventory</a><br>";
exit;}

	//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=\"shops.php?act=sell&id=" . $inventitemid . "\">\n";
		echo "<input type=\"submit\" name=\"sure\" value=\"Yes, I am sure!\" />\n";
		echo "</form>\n";
		break;
	}

if ($row['quantity']== 1)
{
$query = mysql_select("delete from `inventory` where `item_id`=? and `player`=?", array($inventitemid, $player->id));
}
elseif($row['quantity'] > 1)
{
$query = mysql_select("UPDATE `inventory` SET `quantity`=? WHERE `item_id`=? and `player`=?",array($row['quantity'] - 1, $inventitemid, $player->id));
}
	$query = mysql_select("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=\"main.php?username=$x&password=$y&doit=inventory\">Return to inventory</a>";
	break;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/261333-cannot-redeclare-error/
Share on other sites

You are including the posted code more than once. You either have an include/require statement for that file inside of a loop or in more than one other file that is being included, or one of the files you are including in that file is also including that file.

LOL, I was just looking through the remainder of the code, and it is including itself -

 

	if ($do[0] == "buy") { include('shops.php'); buy(); }
elseif ($do[0] == "buy2") { include('shops.php'); buy2($do[1]); }
elseif ($do[0] == "buy3") { include('shops.php'); buy3($do[1]); }
elseif ($do[0] == "sell") { include('shops.php'); sell(); }

 

If you are already running the code on that page, why are you including it? It already exists.

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.