在过去的几天里,我尝试解决了这个问题,但我真的不知道如何处理它。
我有这个inventory.php用于我想要制作的游戏。 类库存工作和它内部的方法也有效。 我的问题是表单循环因为我不知道如何让它正常工作。我希望按钮适用于每个项目,但是有些东西搞砸了,它只适用于第二个(一个或最后一个)。无论按什么按钮,我按下黄金都是第二个按钮。
我知道代码很乱,我确信它可以更容易制作,但我正在尝试解决这个问题。
代码:
<?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->dbh = $dbh;
$this->id = $id;
$this->id_item = $id_item;
$this->equip = $equip;
$this->strength = $strength;
$this->endurance = $endurance;
$this->intelligence = $intelligence;
$this->luck = $luck;
$this->dexterity = $dexterity;
$this->gold = $gold;
$this->gold_sell = $gold_sell;
}
function PrintDetails(){
print "Id:".($this->id)."<br>";
print "Id Item:".($this->id_item)."<br>";
print "Equip:".($this->equip)."<br>";
print "Strength:".($this->strength)."<br>";
print "Endurance:".($this->endurance)."<br>";
print "Intelligence:".($this->intelligence)."<br>";
print "Luck:".($this->luck)."<br>";
print "Dexterity:".($this->dexterity)."<br>";
print "User gold:".($this->gold)."<br>";
print "Sale price:".($this->gold_sell)."<br>";
}
function SellItem(PDO $dbh, $id_item, $gold, $gold_sell){
$this->gold = $this->gold + $this->gold_sell;
print "GOLD :".($this->gold)."<br>";
$sth = $dbh->prepare("UPDATE attributes SET gold = :gold WHERE id = :id");
$sth->bindParam(":gold", $this->gold);
$sth->bindParam(":id", $_SESSION['id']);
$sth->execute();
print "iditr :".($this->id_item)."<br>";
// $inventory->SellItem($dbh, $row2['id_item'],$row3['gold'],$row4['gold_sell']); }
}
}
$sth = $dbh->prepare("SELECT id, username FROM user WHERE id = ".$_SESSION['id']."");
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
$sth3 = $dbh->prepare("SELECT * FROM attributes WHERE id = ".$_SESSION['id']."");
$sth3->execute();
$row3 = $sth3->fetch(PDO::FETCH_ASSOC);
$sth2 = $dbh->prepare("SELECT * FROM inventory WHERE id = ".$_SESSION['id']."");
$sth2->execute();
while ($row2 = $sth2->fetch(PDO::FETCH_ASSOC)){
$sth4 = $dbh->prepare("SELECT * FROM items WHERE id = ".$row2['id_item']."");
$sth4->execute();
$row4 = $sth4->fetch(PDO::FETCH_ASSOC);
print $row2['id'];
print $row2['username'];
print $row2['name']."<br>";
$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->PrintDetails();
$inventory->SellItem($dbh,$row2['id_item'],$row3['gold'],$row4['gold_sell']);
?>
<form id='<?php echo $row2["id_item"]; ?>' name="submit" action="" method="POST">
<button type='submit' value='<?php echo $row2['id_item']; ?>'>Add</button>
</form>
<?php
}
}
如果我查看我的来源,就会出现:
<div id="main">
2motocDagger
<br></br>
Id:2
<br></br>
Id Item:1
<br></br>
Equip:0
<br></br>
Strength:10
<br></br>
Endurance:6
<br></br>
Intelligence:0
<br></br>
Luck:0
<br></br>
Dexterity:3
<br></br>
User gold:202
<br></br>
Sale price:5
<br></br>
GOLD :207
<br></br>
iditr :1
<br></br><form id="1" method="POST" action="" name="submit"><button value="1" type="submit">
Add
</button></form>
2motocStaff
<br></br>
Id:2
<br></br>
Id Item:2
<br></br>
Equip:0
<br></br>
Strength:4
<br></br>
Endurance:5
<br></br>
Intelligence:12
<br></br>
Luck:0
<br></br>
Dexterity:4
<br></br>
User gold:202
<br></br>
Sale price:4
<br></br>
GOLD :206
<br></br>
iditr :2
<br></br><form id="2" method="POST" action="" name="submit"><button value="2" type="submit">
Add
</button></form></div>
表单ID是不同的,所以这不是问题。 如果我点击第一个按钮,我的数据库中的黄金用4金更新,我想让每个按钮工作正常,我想要更新(Dagger's gold)。我怎样才能做到这一点?
对不起,我发了这么大的帖子。 谢谢
答案 0 :(得分:0)
请尝试这样:
<form id='f1' method="POST" action="[setup an url here]" >
<input type="hidden" name="refid" value="1" />
<input type="submit" name="submit" value="Add" />
</form>
然后要求提交这样的内容:
if(isset($_POST['submit'])) {
$refid = intval($_POST['refid']);
// do what you want here..
}