Reputation: 91
I have a problem and I don't know how to sove it.I have an inventory table that contains an id (that is assign to a user)column and id_item column (that is assign to an item from items table) and an items table that also contains an id table.
More specifically this is what my database contains: items table:
id name
1 Dagger
2 Staff
3 Wood Shield
Each with his unique id.
Inventory table:
id id_item username name
1 3 cristi Wood Shield
2 1 motoc Dagger
2 2 motoc Staff
The id is from every user id and id_item is the item's id from items table.
Problem: Let's say I'm logged in as motoc who has 2 weapons in his inventory. Til now everything is fine. I want to make a button for every item that he has. The buttons are there but not working properly. When I click the first one is shows me ssss1 which is correct but when I press the second one nothing hapens. I want to show me ssss2 more specifically the next $row1['id_item'].
I really don't know how to solve this. Thank you.
This is what i've tried:
if (isset($_SESSION['id'])) {
$sth1 = $dbh->prepare("SELECT * FROM inventory WHERE id = ".$_SESSION['id']."");
$sth1->execute();
while($row1 = $sth1->fetch(PDO::FETCH_ASSOC)){
$sth = $dbh->prepare("SELECT * FROM items WHERE id = ".$row1['id_item']."");
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
$ss = print $row1["id_item"];
?>
<form id='<?php echo $row1["id_item"]; ?>' method="POST" action="" >
<input type="hidden" name="refid" value="add" />
<input type="submit" name="submit<?php echo $row1["id_item"]; ?>" value="Add" />
</form>
<?php
}
if (isset($_POST["submit$ss"])) {
$refid = intval($_POST["refid"]);
$sth1 = $dbh->prepare("SELECT * FROM inventory WHERE id = ".$_SESSION['id']."");
$sth1->execute();
$row1 = $sth1->fetch(PDO::FETCH_ASSOC);
echo "ssss".$row1['id_item'];
}
}
Upvotes: 1
Views: 3724
Reputation: 3632
Give this a shot. I'm kinda confused on exactly what you want to happen, but I think this will do it.
<?php
if (isset($_SESSION['id'])) {
$sth1 = $dbh->prepare("SELECT * FROM inventory WHERE id = " . $_SESSION['id']);
$sth1->execute();
while ($row = $sth1->fetch(PDO::FETCH_ASSOC)) {
$sth = $dbh->prepare("SELECT * FROM items WHERE id = " . $row['id_item']);
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
$ss = $row["id_item"];
?>
<form id='<?php echo $ss; ?>' method="post" action="?show">
<input type="hidden" name="item_id" value="<?php echo $ss; ?>" />
<input type="submit" name="submit" value="Add" />
</form>
<?php
}
if (isset($_GET["show"]) && isset($_POST['item_id'])) {
echo "ssss" . $_POST['item_id'];
}
}
I cleaned up some of the code and changed the way the form was built. I also changed the PHP code at the bottom to check for the changes in the form.
I will tell you now though. The way you designed the database should be changed. Keeping that updated will be a pain in the ass. You should use an items table, a users table, and have a pivot table between them since it is a many-to-many relationship.
Have fun!
Upvotes: 0
Reputation: 360842
This is a bad way of building your form. Since you're building a "personalized" form for EVERY item, there's no need to create dynamic field names, just a hidden form field:
<form ... >
<input type="hidden" name="id_item" value="<?php echo $row1['id_item'] ?>" />
<input type="hidden" name="refid" value="add" />
<input type="submit" name="submit" value="Add" />
</form>
Then you simply check $_POST['id_item']
in the form handling code, instead of having to look for every single possible submit1
, submit2
, etc...
As well, your form handling code is running within the same context as the form generation code, before the form has even had a chance to be displayed and get a user click. You should at least have somethign like
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... handle form here ...
echo "ssss...";
}
so the item info retrieval only runs when the form actually HAS been submitted.
Upvotes: 3