gpt4 book ai didi

php - 将 mysql 结果链接到另一个

转载 作者:行者123 更新时间:2023-11-29 18:22:04 25 4
gpt4 key购买 nike

抱歉,这个问题令人困惑,我是一个初学者,正在寻求对我的代码进行一些改进。

我目前正在尝试做的是将一个 mysql 值“链接”到另一个值。这是一个例子:

  • 我正在创建一个基于 PHP 的战斗
  • 有一个名为“fightm”的数据库,其中包含重要信息
  • 一些信息是攻击冷却时间
  • 我想通过将 ATTACKCOOLDOWN 链接在一起来简化我的代码

为了澄清,我会给你一些代码(不要忘记我是初学者,如果写得不好你可以纠正我)

        if (isset($_POST['fdd_attackbas1']) AND ($infomonstre["fightm_cdbas1"] < 1)) {
$query = $db->prepare('UPDATE users SET vieac=(vieac - :damage ) WHERE username=:username');
$query->bindValue(':username', $username, PDO::PARAM_INT);
$query->bindValue(':damage', $damage, PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
$query = $db->prepare('UPDATE fightmonster SET
fightm_life=(fightm_life - :atqb),
fightm_dmgperso=:atqb,
fightm_dmgenemy=:damage,
fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1),
fightm_cdbas1=(fightm_cdbas1 + 1),
fightm_cdbas2=GREATEST(0, fightm_cdbas2 - 1),
fightm_cdbas3=GREATEST(0, fightm_cdbas3 - 1),
fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1),
fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1),
fightm_cdultime=GREATEST(0, fightm_cdultime - 1)
WHERE player_id =:id');
$query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
$query->bindValue(':damage', $degatreduiteffet, PDO::PARAM_INT);
$query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
header("Refresh:0");
exit();
}

if (isset($_POST['fdd_attackbas2']) AND ($infomonstre["fightm_cdbas2"] < 1)) {
$query = $db->prepare('UPDATE users SET vieac=(vieac - :damage ) WHERE username=:username');
$query->bindValue(':username', $username, PDO::PARAM_INT);
$query->bindValue(':damage', $damage, PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
$query = $db->prepare('UPDATE fightmonster SET
fightm_life=(fightm_life - :atqb),
fightm_dmgperso=:atqb,
fightm_dmgenemy=:damage,
fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1),
fightm_cdbas1=GREATEST(0, fightm_cdbas1 - 1),
fightm_cdbas2=(fightm_cdbas2 + 1),
fightm_cdbas3=GREATEST(0, fightm_cdbas3 - 1),
fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1),
fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1),
fightm_cdultime=GREATEST(0, fightm_cdultime - 1)
WHERE player_id =:id');
$query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
$query->bindValue(':damage', $degatreduiteffet, PDO::PARAM_INT);
$query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
header("Refresh:0");
exit();
}

如您所见,我对每次“攻击”都重复自己。现在,想象一下我有超过 20 次攻击!那太疯狂了。

我想做的是这样的事情(我想要的代码,但带有我不知道的代码部分的注释)

        if (isset(//One of the attack//) AND (// The CD linked to the attack //)) {
$query = $db->prepare('UPDATE users SET vieac=(vieac - :damage ) WHERE username=:username');
$query->bindValue(':username', $username, PDO::PARAM_INT);
$query->bindValue(':damage', $damage, PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
$query = $db->prepare('UPDATE fightmonster SET
fightm_life=(fightm_life - :atqb),
fightm_dmgperso=:atqb,
fightm_dmgenemy=:damage,
// COOLDOWN PART, SAME BUT WITHOUT NEEDING TO CHANGE //
WHERE player_id =:id');
$query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
$query->bindValue(':damage', $degatreduiteffet, PDO::PARAM_INT);
$query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
$query->execute();
$query->CloseCursor();
header("Refresh:0");
exit();
}

这会很有帮助,但我不明白如何做到这一点。谢谢您,抱歉文字太长!

最佳答案

将代码重构为 foreach 循环,如下所示:

$keys = ['fdd_attackbas1', 'fdd_attackbas2', ..];
foreach ($keys as $key) {
if (isset[$key] && ..) {
..
exit();
}
}

关于php - 将 mysql 结果链接到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46494233/

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