0

So... Star Wars code again...

I have some code that I want to change a boolean value in a MySQL database from true to false, based on a button push. The specific code included relates to people who have stated that their chosen character uses a lightsaber. I want to apply a flag to a view called "gameview" whose rows will be counted until there is one character left. If there's a false value in the flag column, then it is not counted.

I have created a number of classes, methods and controllers as well as a view. The pages all display as they should. However, when the relevant button is pressed, the code is not changing the flags as expected. I am fairly sure that the MySQL code is good, so it is something to do with the PHP code. The method is in a class called gameview.class.php

Class Codes:

Public function iterateGameView($id)
    {   $changeFlag = "UPDATE gameview SET flag :flag WHERE id = :id";
        $stmt = $this->Conn->prepare($changeFlag);
        $stmt->execute(array([':flag' => false, ':id' => [$id]]));
    }

Lightsaber Class Code

public function noLightsaberUsed()
    {
        $query = "select * from person, weapon, person_weapon 
WHERE flag = 0 AND person.id = person_weapon.person_id AND person_weapon.weapon_id = weapon.id and weapon.name != 'Lightsaber'
AND person.name NOT IN (select person.name from person, weapon, person_weapon WHERE person.id = person_weapon.person_id
AND person_weapon.weapon_id = weapon.id and weapon.name = 'Lightsaber')";
        $stmt = $this->Conn->prepare($query);
        $stmt->execute(array());
        return $stmt->fetchAll(PDO::FETCH_ASSOC);

    }

Lightsaber controller

<?php
$lightsaber = new lightsaber($Conn);
$gameView = new gameView($Conn);

if ($_POST) {
    if ($_POST['lightsaber'] = 1) {
        $lightsabers = $lightsaber->noLightsaberUsed();
        $smarty->assign('lightsabers_used', $lightsabers);
        $lightsabersFlag = array($lightsabers, 'id', 'flag');
        for ($i = 0; $i <= count($lightsabersFlag); $i++) {
            $gameView->iterateGameView($lightsabersFlag[$i]);

        }

    } elseif ($_POST['lightsaber'] = 2) {
        $lightsabers = $lightsaber->lightsaberUsed();
        $smarty->assign('lightsabers_used', $lightsabers);
        $lightsabersFlag = array($lightsabers, 'id', 'flag');
        for ($i = 0; $i <= count($lightsabersFlag); $i++) {
            $gameView->iterateGameView($lightsabersFlag[$i]);
        }
    }

    $count = $gameView->countGameView();
    if ($count > 1) {
        header("Location: index.php?p=rebel");
    } elseif ($count < 1) {
        header("Location: index.php?p=fail");
    } else {
        header("Location: index.php?p=gameview");
    }

}
?>

The bit that I believe that I am stuck on is the iterateGameView method. I'm a relative novice with PHP and MySQL and this is the first time I've attempted a prepared statement.

Stevie
  • 31
  • 7
  • `$_POST['lightsaber'] = 1` - You're assigning 1 instead of comparing – aynber Feb 14 '20 at 13:40
  • Apologies.... this refers to code in a template where a button is assigned a value of "1". – Stevie Feb 14 '20 at 13:45
  • Also, you're adding an extra array element that is probably throwing off your query. `[':flag' => false, ':id' => [$id]]` – aynber Feb 14 '20 at 13:48
  • The confusion that I have is that I have an array variable called $lightsabersFlag, and I want to use that array to set the flags in my main view. I cannot for the life of me seem to be able to create the code that will do that. I know what I want it to do: UPDATE gameview SET flag = false where id (is in $lightsaberFlag) but no amount of of research for this novice is helping. – Stevie Feb 14 '20 at 13:59
  • _“this refers to code in a template where a button is assigned a value of "1"”_ - if it was a mere assignment, then why is it wrapped in an `if`? – misorude Feb 14 '20 at 14:06
  • Hi Misorude: Because I haven't posted the entire code. There are 2 buttons on the page, one is assigned 1 and the other 2. if button Yes is clicked, the first code runs, if No is clicked, different code is run. Simples! – Stevie Feb 14 '20 at 14:09
  • At the moment, the "no" option will never run because you're assigning 1 to `$_POST['lightsaber']` instead of comparing it. The other issue is the update query I mentioned in my second comment. Remove the brackets from `[$id]` – aynber Feb 14 '20 at 14:10
  • I will update the code, as it's wrapped in another if statement. – Stevie Feb 14 '20 at 14:11
  • Also, I'm not sure `$lightsabersFlag` is doing what you want it to do. `$lightsabers` is at least 1 row, so that's an array. This means that `$lightsabersFlag` contains an array inside of an array. Your `for` is iterating through `$lightsabersFlag`, so the first loop would be `$lightsabers` (a multidimensional array), the second would be `id`, and the third would be `flag` – aynber Feb 14 '20 at 14:13
  • The best way to troubleshoot is taking advantage of logging or `var_dump`. Find out what variables are being passed along the way so you can make sure it matches what you think it should be. – aynber Feb 14 '20 at 14:14
  • OK, that kind of makes sense. So as my $lightsabers is already an array, the $lightsaberFlag is superfluous to needs. – Stevie Feb 14 '20 at 14:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207844/discussion-between-stevie-and-aynber). – Stevie Feb 14 '20 at 14:32

0 Answers0