Note that there are some explanatory texts on larger screens.

plurals
  1. POForm inside loop not working properly
    primarykey
    data
    text
    <p>In the last couple of days I just tried and tried to resolve this problem but I really don't know how to deal with it.</p> <p>I have this inventory.php for a game that I'm trying to make. The class Inventory works and methods inside it works too. My problem is with the form loop beacause I don't know how to make it work properly. I want the buttons to work for every item but something is messed up and it works just for the second (one or last one).Doesn't matter what button I press the gold is taken for the second button.</p> <p>I know the code is messy and I'm sure it could be made more easly but I'm trying to resolve this problem. </p> <p>The code:</p> <pre><code>&lt;?php if (isset($_SESSION['id'])) { /** * Inventory */ class Inventory { public $dbh; public $id; public $id_item; public $equip; public $strength; public $endurance; public $intelligence; public $luck; public $dexterity; public $gold; public $gold_sell; function __construct(PDO $dbh, $id, $id_item, $equip, $strength, $endurance, $intelligence, $luck, $dexterity, $gold, $gold_sell) { $this-&gt;dbh = $dbh; $this-&gt;id = $id; $this-&gt;id_item = $id_item; $this-&gt;equip = $equip; $this-&gt;strength = $strength; $this-&gt;endurance = $endurance; $this-&gt;intelligence = $intelligence; $this-&gt;luck = $luck; $this-&gt;dexterity = $dexterity; $this-&gt;gold = $gold; $this-&gt;gold_sell = $gold_sell; } function PrintDetails(){ print "Id:".($this-&gt;id)."&lt;br&gt;"; print "Id Item:".($this-&gt;id_item)."&lt;br&gt;"; print "Equip:".($this-&gt;equip)."&lt;br&gt;"; print "Strength:".($this-&gt;strength)."&lt;br&gt;"; print "Endurance:".($this-&gt;endurance)."&lt;br&gt;"; print "Intelligence:".($this-&gt;intelligence)."&lt;br&gt;"; print "Luck:".($this-&gt;luck)."&lt;br&gt;"; print "Dexterity:".($this-&gt;dexterity)."&lt;br&gt;"; print "User gold:".($this-&gt;gold)."&lt;br&gt;"; print "Sale price:".($this-&gt;gold_sell)."&lt;br&gt;"; } function SellItem(PDO $dbh, $id_item, $gold, $gold_sell){ $this-&gt;gold = $this-&gt;gold + $this-&gt;gold_sell; print "GOLD :".($this-&gt;gold)."&lt;br&gt;"; $sth = $dbh-&gt;prepare("UPDATE attributes SET gold = :gold WHERE id = :id"); $sth-&gt;bindParam(":gold", $this-&gt;gold); $sth-&gt;bindParam(":id", $_SESSION['id']); $sth-&gt;execute(); print "iditr :".($this-&gt;id_item)."&lt;br&gt;"; // $inventory-&gt;SellItem($dbh, $row2['id_item'],$row3['gold'],$row4['gold_sell']); } } } $sth = $dbh-&gt;prepare("SELECT id, username FROM user WHERE id = ".$_SESSION['id'].""); $sth-&gt;execute(); $row = $sth-&gt;fetch(PDO::FETCH_ASSOC); $sth3 = $dbh-&gt;prepare("SELECT * FROM attributes WHERE id = ".$_SESSION['id'].""); $sth3-&gt;execute(); $row3 = $sth3-&gt;fetch(PDO::FETCH_ASSOC); $sth2 = $dbh-&gt;prepare("SELECT * FROM inventory WHERE id = ".$_SESSION['id'].""); $sth2-&gt;execute(); while ($row2 = $sth2-&gt;fetch(PDO::FETCH_ASSOC)){ $sth4 = $dbh-&gt;prepare("SELECT * FROM items WHERE id = ".$row2['id_item'].""); $sth4-&gt;execute(); $row4 = $sth4-&gt;fetch(PDO::FETCH_ASSOC); print $row2['id']; print $row2['username']; print $row2['name']."&lt;br&gt;"; $inventory = new Inventory($dbh,$row['id'],$row2['id_item'],$row2['equip'],$row4['strength'],$row4['endurance'],$row4['intelligence'],$row4['luck'],$row4['dexterity'],$row3['gold'],$row4['gold_sell']); $inventory-&gt;PrintDetails(); $inventory-&gt;SellItem($dbh,$row2['id_item'],$row3['gold'],$row4['gold_sell']); ?&gt; &lt;form id='&lt;?php echo $row2["id_item"]; ?&gt;' name="submit" action="" method="POST"&gt; &lt;button type='submit' value='&lt;?php echo $row2['id_item']; ?&gt;'&gt;Add&lt;/button&gt; &lt;/form&gt; &lt;?php } } </code></pre> <p>If I view my source this comes up:</p> <pre><code>&lt;div id="main"&gt; 2motocDagger &lt;br&gt;&lt;/br&gt; Id:2 &lt;br&gt;&lt;/br&gt; Id Item:1 &lt;br&gt;&lt;/br&gt; Equip:0 &lt;br&gt;&lt;/br&gt; Strength:10 &lt;br&gt;&lt;/br&gt; Endurance:6 &lt;br&gt;&lt;/br&gt; Intelligence:0 &lt;br&gt;&lt;/br&gt; Luck:0 &lt;br&gt;&lt;/br&gt; Dexterity:3 &lt;br&gt;&lt;/br&gt; User gold:202 &lt;br&gt;&lt;/br&gt; Sale price:5 &lt;br&gt;&lt;/br&gt; GOLD :207 &lt;br&gt;&lt;/br&gt; iditr :1 &lt;br&gt;&lt;/br&gt;&lt;form id="1" method="POST" action="" name="submit"&gt;&lt;button value="1" type="submit"&gt; Add &lt;/button&gt;&lt;/form&gt; 2motocStaff &lt;br&gt;&lt;/br&gt; Id:2 &lt;br&gt;&lt;/br&gt; Id Item:2 &lt;br&gt;&lt;/br&gt; Equip:0 &lt;br&gt;&lt;/br&gt; Strength:4 &lt;br&gt;&lt;/br&gt; Endurance:5 &lt;br&gt;&lt;/br&gt; Intelligence:12 &lt;br&gt;&lt;/br&gt; Luck:0 &lt;br&gt;&lt;/br&gt; Dexterity:4 &lt;br&gt;&lt;/br&gt; User gold:202 &lt;br&gt;&lt;/br&gt; Sale price:4 &lt;br&gt;&lt;/br&gt; GOLD :206 &lt;br&gt;&lt;/br&gt; iditr :2 &lt;br&gt;&lt;/br&gt;&lt;form id="2" method="POST" action="" name="submit"&gt;&lt;button value="2" type="submit"&gt; Add &lt;/button&gt;&lt;/form&gt;&lt;/div&gt; </code></pre> <p>The form id's are different so that is not the problem. If I click the first button the gold inside my database is updated with 4 gold and I want to make every button to work properlt.5 gold I want to be updated (Dagger's gold). How can I do that? </p> <p>I'm sorry that I made such a big post. Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload