微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

foreach正确运行一次正确

如何解决foreach正确运行一次正确

所以我刚刚开始学习PDO,无法弄清楚这一点。

我遇到的问题是,循环一次正确运行(似乎至少)所有内容添加到了正确的数据库中,但是随后似乎没有从数组的其余部分获取“ dkpAmount”,并且也不会向其中添加新的dkpAmount。

请帮助。

我的表单如下:

    <form class="upd-dkp" method="POST" action="inc/update-dkp.inc.PHP">
    <textarea placeholder="One,Two,Three" id="characters" type="text" class="validate" name="characters"></textarea>
        <input placeholder="10" id="defeated" type="text" class="validate" name="defeated">
        <input placeholder="2" id="wipes" type="text" class="validate" name="wipes">
        <select name="raid">
            <option value="1">Molten Core</option>
            <option value="2">Blackwing Lair</option>
            <option value="3">Ahn'Qiraj</option>
            <option value="4" disabled>Naxxramas</option>
        </select>

        <button class="btn waves-effect waves-light" type="submit" name="submit">Update</button>
</form>

和我的update-dkp.inc.PHP看起来像这样:

<?PHP
include 'dbh.PHP';
    
$charname = $newdkp = $dkpAmo = '';

if(isset($_POST['submit'])){
    require_once 'functions.inc.PHP';
    $raiders = explode(',',trim($_POST['characters']));
    $defeated = $_POST['defeated'];
    $wipes = $_POST['wipes'];

            if($_POST['raid'] == 1) {
                foreach($raiders as $raider) {
                    $newDKP = round((5 * $defeated) - (6 * $wipes));
                    $raid = 'Molten Core';
                 
                    $stmt = $handler->prepare('UPDATE userchars SET dkpAmount = dkpAmount + :newdkp WHERE charName = :charname');
                    $stmt->bindValue(':charname',$raider);
                    $stmt->bindValue(':newdkp',$newDKP);
                    $stmt->execute();
                    
                    // adding to history db
                    $dkpAmount = $handler->prepare('SELECT dkpAmount from userchars WHERE charName = :charname');
                    $dkpAmount->bindValue(':charname',$raider);
                    $dkpAmount->execute();
                    $dkpAmount = $dkpAmount->fetchAll();
                    $dkpAmo = $dkpAmount;
        
                    $hist = $handler->prepare('INSERT INTO dkp_history(raidName,dkpInc,dkpAmo,charname,histdate) VALUES(:raid,:dkpInc,:dkpAmo,:charname,:histdate)');
                    
                    $hist->bindValue(':raid',$raid);
                    $hist->bindValue(':dkpInc',$newDKP);
                    $hist->bindValue(':dkpAmo',$dkpAmo);
                    $hist->bindValue(':charname',$raider);
                    $hist->bindValue(':histdate',date("Y-m-d H:i:s"));
                    
                    $hist->execute();
                }
            }
            if($_POST['raid'] == 2) {
                foreach($raiders as $raider) {
                    $newDKP = round((5 * $defeated) - (6 * $wipes) + 35);
                    $raid = 'Blackwing Lair';

                    updateDKP($handler,$raider,$newDKP);

                    getCurrentDKP($handler,$raider);

                    insertHistory($handler,$raid,$newDKP,$dkpAmo,$raider);
                }
            }
            if($_POST['raid'] == 3) {
                foreach($raiders as $raider) {
                    $newDKP = (5 * $defeated) - (6 * $wipes) + 55;
                    $raid = 'Ahn\'Qiraj';
                   
                    $stmt = $handler->prepare('UPDATE userchars SET dkpAmount = dkpAmount + :newdkp WHERE charName = :charname');
                    $stmt->bindValue(':charname',$newDKP);
                    $stmt->execute();
                    
                    // adding to history db
                    $dkpAmount = $handler->prepare('SELECT dkpAmount from userchars WHERE charName = :name');
                    $dkpAmount->bindValue(':name',$raider);
                    $dkpAmount->execute();
                    $dkpAmount = $dkpAmount->fetch(PDO::FETCH_ASSOC);
                    $dkpAmo = $dkpAmount;
        
                    $hist = $handler->prepare('INSERT INTO dkp_history(raidName,date("Y-m-d H:i:s"));
                    
                    $hist->execute(); 
                }
            }
            if($_POST['raid'] == 4) {
                foreach($raiders as $raider) {
                    $newDKP = (5 * $defeated) - (6 * $wipes) + 75;
                    $raid = 'Naxxramas';
                   
                    $stmt = $handler->prepare('UPDATE userchars SET dkpAmount = dkpAmount + :newdkp WHERE charName = :charname');
                    $stmt->bindValue(':charname',$raider);
                    $dkpAmount->execute();
                    $dkpAmount = $dkpAmount->fetch(PDO::FETCH_ASSOC);
                    $dkpAmo = $dkpAmount['dkpAmount'];
        
                    $hist = $handler->prepare('INSERT INTO dkp_history(raidName,date("Y-m-d H:i:s"));
                    
                    $hist->execute(); 
            }
        }
        header("Location: ../index.PHP?dkp=".$newDKP."");
    } else {
        header("Location: ../index.PHP");
    }
?>

我的函数中运行的代码与其他3个代码基本相同,但我还是会发布它:

    function updateDKP($handler,$newDKP) {
    $stmt = $handler->prepare('UPDATE userchars SET dkpAmount = dkpAmount + :newdkp WHERE charName = :charname');
    $stmt->bindValue(':charname',$raider);
    $stmt->bindValue(':newdkp',$newDKP);
    $stmt->execute();
}
function getCurrentDKP($handler,$raider) {
    $dkpAmount;
    $dkpAmo;
    $dkpAmount = $handler->prepare('SELECT dkpAmount FROM userchars WHERE charName = :charname');
    $dkpAmount->bindValue(':charname',$raider);
    $dkpAmount->execute();
    $dkpAmount = $dkpAmount->fetch(PDO::FETCH_ASSOC);
    $dkpAmo = $dkpAmount['dkpAmount'];
    return $dkpAmo;
}
function insertHistory($handler,$raider) {
    $hist = $handler->prepare('INSERT INTO dkp_history(raidName,:histdate)');
    
    $hist->bindValue(':raid',$raid);
    $hist->bindValue(':dkpInc',$newDKP);
    $hist->bindValue(':dkpAmo',$dkpAmo);
    $hist->bindValue(':charname',$raider);
    $hist->bindValue(':histdate',date("Y-m-d H:i:s"));
    
    $hist->execute();
}

我知道这在页面上变成了很多代码。但不想让您缺少任何帮助^^

*编辑: 还要提及的是,它确实以所有正确的名称添加到dkp_history中,$ dkpInc在那里也是正确的-它只是不抓住$ dkpAmount或更新$ dkpAmount

评论中的解决方案->试图具有描述性。

解决方法

解决方案:

因此,解决方案易于实施,但对我而言却很难找到。在我的两次尝试中都使用$ _POST ['raid'] = 2。

解决方案1。 该数组在每行中除第一行外都有空格,这就是为什么第一个foreach()顺利通过的原因。为了解决这个问题,我尝试了一些操作,例如trim&然后爆炸,但似乎并没有消除空格。我最终要做的是:

$raiderx = urlencode($_POST['characters']);
$raiders = str_replace("+","",$raiderx);
$raiders = urldecode($raiders);
$raiders = explode(',',$raiders);

urlencode()-将空格转换为“ +”,因此可以毫无问题地进入url,然后从那里我将+替换为空。然后,我使用explode()再次将其放入数组,并且空格不再存在。

解决方案2.运行该功能时:

getCurrentDKP($handler,$raider);

我以前忘记将其设置为变量。所以我做到了,它是固定的。

$dkpAmo = getCurrentDKP($handler,$raider);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。