循环内的PHP按钮 [英] PHP Buttons inside loop
问题描述
我有一个问题,我不知道如何解决.我有一个库存表,其中包含一个ID(分配给用户)列和ID_item列(该分配给项目表中的项目)还有一个项目表,其中还包含一个ID表.
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
每个人都有自己的唯一ID.
Each with his unique id.
库存表:
id id_item username name
1 3 cristi Wood Shield
2 1 motoc Dagger
2 2 motoc Staff
该ID来自每个用户ID,而id_item是来自item表的该item的id.
The id is from every user id and id_item is the item's id from items table.
问题:假设我以motoc身份登录,他的库存中有2种武器.到现在一切都很好.我要为他拥有的每个物品都按下一个按钮.按钮在那里,但不能正常工作.当我单击第一个时,显示ssss1是正确的,但是当我按第二个时,则没有发生.我想更具体地向我展示ssss2,下一个$ row1 ['id_item'].
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.
这是我尝试过的:
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'];
}
}
推荐答案
这是构建表单的一种不好的方法.由于您要为每个项目都构建一个个性化"表单,因此无需创建动态字段名称,只需创建一个隐藏的表单字段即可:
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>
然后,您只需在表单处理代码中检查 $ _ POST ['id_item']
,而不必查找每个可能的 submit1
, submit2
等...
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.
这篇关于循环内的PHP按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!