While loop with FORM help *SOLVED*
Posted: Mon Jul 18, 2011 9:28 pm
I need someone with fresh eyes to look over the code below. I took a break from my main project to work on a smaller demo application for someone and it's like I've suddenly become retarded. I've done the form submission from inside a while about 100 times and have never had issues. So i figure I'm missing something minor or else I wouldn't be throwing chairs across my living room.
The problem is that no matter which item returned through the while loop you click BUY on, it always sends POST of the last item in the table.
The code works if I use the below to process the purchase but the problem with it is that it purchases two of the items instead of 1
The problem is that no matter which item returned through the while loop you click BUY on, it always sends POST of the last item in the table.
The code works if I use the below to process the purchase but the problem with it is that it purchases two of the items instead of 1
Code: Select all
<a href='shop.php?purchasepart&item=$partinfo3[iid]&quantity=1'>BUY</a>Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Monster Mash-up</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<?php
include_once 'facebook.php';
include_once 'connect.php';
include_once 'fbprocess.php';
$playerinfo="SELECT * from players WHERE fbuid='$fbme[id]'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
if(empty($playerinfo3))
{
echo "The game is currently down for redesign.";
//exit;
}
?>
<?php include 'navbar.php';?>
<div id='mainContent'>
<p></p>
<div id='statusDisplay'>
<?php
if (isset($_POST['purchasepart']))
{
$item = $_POST['item'];
$quantity = $_POST['quantity'];
echo "ID: $item Quantity: $quantity <br>";
$iteminfo="SELECT * from partitems where iid='$item'";
$iteminfo2=mysql_query($iteminfo) or die("could not get item stats!");
$iteminfo3=mysql_fetch_array($iteminfo2);
if($iteminfo3['gold'] < $playerinfo3['gold'])
{
$iid = $iteminfo3['iid'];
$name = $iteminfo3['name'];
$location = $iteminfo3['location'];
$level = $iteminfo3['level'];
$hpoints = $iteminfo3['hpoints'];
$energy = $iteminfo3['energy'];
$strength = $iteminfo3['strength'];
$speed = $iteminfo3['speed'];
$ability = $iteminfo3['ability'];
$gold = $iteminfo3['gold'];
$coins = $iteminfo3['coins'];
$rarity = $iteminfo3['rarity'];
$bind = $iteminfo3['bind'];
$tradeable = $iteminfo3['tradeable'];
$description = $iteminfo3['description'];
$img = $iteminfo3['img'];
$itembought = "INSERT into playerparts(iid,pid,name,location,level,hpoints,energy,strength,speed,ability,gold,coins,rarity,bind,tradeable,equipped,description,img) VALUES ('$iid','$playerinfo3[fbuid]','$name','$location','$level','$hpoints','$energy','$strength','$speed','$ability','$gold','$coins','$rarity','$bind','$tradeable','0','$description','$img')";
mysql_query($itembought) or die(mysql_error());
$updatetarget="UPDATE players SET gold=gold-'$iteminfo3[gold]' WHERE pid='$fbme[id]'";
mysql_query($updatetarget) or die(mysql_error());
echo "<font color='green'>$iteminfo3[name] purchased for $iteminfo3[gold] gold.</font><br>";
}
else
{
echo "<font color='green'>You did not have enough money to purchase the item.</font><br>";
}
}
?>
</div>
<p></p>
<?php
// pull parts items
echo "Parts";
echo "<br>";
$partinfo="SELECT * from partitems";
$partinfo2=mysql_query($partinfo) or die("could not find items!");
while($partinfo3=mysql_fetch_array($partinfo2))
{
echo "<div id='content'>";
echo "<div id='itemHeader'><b>$partinfo3[name]</b></div>";
echo "<table border='0'>";
echo "<tr>";
echo "<td valign='top' width='125'><div id='itemImage'><img src='$partinfo3[img]' height='125'></div></td>";
echo "<td valign='top' width='150' align='left'>HP: $partinfo3[hpoints]<br> E:$partinfo3[energy] <br> id:$partinfo3[iid]</td>";
echo "<td valign='top' width='150' align='left'>Str: $partinfo3[strength]<br> Spd: $partinfo3[speed]</td>";
echo "<td valign='top' width='200' align='left'><div>$partinfo3[description]</div></td>";
echo "</tr>";
echo "</table>";
echo "<div id='itemFooter'>Gold: $partinfo3[gold] <form action='shop.php' method='post'><input type='hidden' name='name' value='$partinfo3[name]'><input type='hidden' name='item' value='$partinfo3[iid]'><input type='hidden' name='quantity' value='1'><input type='submit' name='purchasepart' value='$partinfo3[iid]'> Credits: $partinfo3[coins] <a href=''>BUY</a></div>";
echo "</div>";
}
?>
</div>