将mysql结果链接到另一个

时间:2017-09-29 17:38:52

标签: php mysql

很抱歉这个令人困惑的问题,我是初学者,希望对我的代码进行一些改进。

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

  • 我正在创建一个基于PHP的战斗
  • 有一个名为“fightm”的数据库,其中包含重要信息
  • 部分信息是攻击 COOLDOWN
  • 我希望通过将攻击 COOLDOWN 链接在一起来简化我的代码

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

        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();
}

这会非常有帮助,但我无法理解如何做到这一点。 谢谢你,抱歉长文!

1 个答案:

答案 0 :(得分:0)

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

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