【问题标题】:foreach() running correct once correctlyforeach() 正确运行一次
【发布时间】:2021-02-18 04:36:29
【问题描述】:

所以我刚开始学习 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', $raider);
                    $stmt->bindValue(':newdkp', $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, 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'] == 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);
                    $stmt->bindValue(':newdkp', $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['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(); 
            }
        }
        header("Location: ../index.php?dkp=".$newDKP."");
    } else {
        header("Location: ../index.php");
    }
?>

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

    function updateDKP($handler, $raider, $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, $raid, $newDKP, $dkpAmo, $raider) {
    $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();
}

我知道这变成了页面上的大量代码。但不希望有任何遗漏让您能够提供帮助^^

*编辑: 还想提一下,它确实使用所有正确的名称添加到 dkp_history,$dkpInc 在那里也是正确的 - 它只是没有获取 $dkpAmount 或更新 $dkpAmount

cmets 中的解决方案 -> 试图描述性。

【问题讨论】:

  • 为什么您认为这个问题甚至与 PDO 有关? 调用任何数据库交互之前,您是否确保所有值都已正确设置?
  • 好吧,至少我可以发现一个问题,$dkpAmount 显然是一个数组,因此按原样插入 int 是没有意义的。但是,Stack Overflow 并不是“查看我的代码并使其工作”的服务。您不仅应该编写一些代码墙,还应该验证它是否有效。然后提出一个具体的问题。
  • @YourCommonSense - 你是对的,我很抱歉。就这样坐了一晚上,真的不知道如何正确地问这个问题。
  • 你不应该一次写这么多的代码。您必须分小部分编写并强制验证每个部分,然后再转到另一个部分。在每次插入查询之前,您必须检查要插入的每个值。
  • 你说循环运行正确,但是哪个?你有4个不同的循环,你的问题太模糊了,应该更加集中,用错误的必要代码编辑问题,但在此之前你应该首先检查你的变量包含什么。

标签: php pdo foreach


【解决方案1】:

解决方案:

所以解决方案很容易实施,但对我来说很难找到。在我的两次尝试中都在使用 $_POST['raid'] = 2。

解决方案 1。 除了第一行之外,数组的每一行都有空格,这就是为什么第一个 foreach() 没有任何问题通过的原因。为了解决这个问题,我尝试了一些东西,比如修剪然后爆炸,但它似乎并没有摆脱空间。我最终做了什么:

$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);

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多