Update MySQL table with foreach passing multiple list values

Viewed 37

I'm actually running into an issue when trying to put this into practice. So, I have a list of goal ids and a current amount. I need to pass all these values which I am by doing goalidlist[] and current[]. The issue is when I'm running the foreach and doing the SQL Update per goalid. Here's my code below. Hope someone can point out what I'm doing wrong.

$goalidlist = $_POST['goalidlist'];
$currentnums = $_POST['current'];
if (isset($_POST['goalidlist'])) {

    foreach($goalidlist as $key => $glist) {
        
        // Update key goals
        $updGoals = "UPDATE okr_3519031jkl.key_goals SET current=? WHERE goalid=? AND userid=?";
        
        if ($stmt = mysqli_prepare($conn, $updGoals)) {
            // Bind Params
            mysqli_stmt_bind_param($stmt, "sii", $currentnums[$key], $glist, $userid);
            mysqli_stmt_execute($stmt);
            $stmt->close();

            
        }
    }
}

goalidlist[] = Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 6 [6] => 9 [7] => 7 [8] => 8 [9] => 10 [10] => 11 [11] => 12 [12] => 13 )

current[] = Array ( [0] => 3 [1] => 1 [2] => 2 [3] => 6 [4] => 2 [5] => 12 [6] => 3 [7] => 1 [8] => 7 [9] => 9 [10] => 2 [11] => 6 [12] => 3 )

0 Answers
Related