gpt4 book ai didi

php - 循环内的表单无法正常工作

转载 作者:行者123 更新时间:2023-11-30 01:28:53 26 4
gpt4 key购买 nike

在过去的几天里,我一直在尝试解决这个问题,但我真的不知道如何处理。

我有一个我正在尝试制作的游戏的 inventory.php。Inventory 类可以工作,它里面的方法也可以工作。我的问题是表单循环,因为我不知道如何使其正常工作。我希望这些按钮适用于每个项目,但有些东西搞砸了,它只适用于第二个(一个或最后一个)。无论我按哪个按钮,第二个按钮都会采用金色。

我知道代码很困惑,我确信它可以变得更容易,但我正在努力解决这个问题。

代码:

<?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 金币,并且我想让每个按钮都能正常工作。我想要更新 5 金币( Dagger 的金币)。我怎样才能做到这一点?

很抱歉我发了这么大的帖子。谢谢

最佳答案

请尝试这样:

<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..
}

关于php - 循环内的表单无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com