Exemple #1
0
 public static function saveWinners($db)
 {
     if (is_array($_POST['winners'])) {
         $sql = "UPDATE winners SET winners.price = :price, leftToPay = :price WHERE winners.id = :id";
         $stmt = $db->prepare($sql);
         $stmt->bindParam(":price", $price, PDO::PARAM_INT);
         $stmt->bindParam(":id", $id, PDO::PARAM_INT);
         foreach ($_POST['winners'] as $winner) {
             $price = $winner['price'];
             $id = $winner['id'];
             $stmt->execute();
         }
     }
     $sql = "SELECT players.id, places.place FROM players INNER JOIN places ON " . "places.id = players.place WHERE players.name = :name LIMIT 1";
     $stmt = $db->prepare($sql);
     $stmt->bindValue(":name", $_POST['winner']['name'], PDO::PARAM_STR);
     $stmt->execute();
     $winner = $stmt->fetch(PDO::FETCH_ASSOC);
     $sql = "SELECT places.id FROM places WHERE place = :place";
     $stmt = $db->prepare($sql);
     $stmt->bindValue(":place", $_POST['winner']['place'], PDO::PARAM_STR);
     $stmt->execute();
     $place = $stmt->fetchColumn(0);
     if ($place == false) {
         // place doesn't exist
         $sql = "INSERT INTO places (place) VALUES(:place)";
         $stmt = $db->prepare($sql);
         $stmt->bindValue(":place", $_POST['winner']['place'], PDO::PARAM_STR);
         $stmt->execute();
         $place = $db->lastInsertId();
     }
     if ($winner['place'] != $_POST['winner']['place']) {
         // update the place
         $sql = "UPDATE players SET place = :place WHERE id = :id";
         $stmt = $db->prepare($sql);
         $stmt->bindValue(":place", $place, PDO::PARAM_INT);
         $stmt->bindValue(":id", $winner['id'], PDO::PARAM_INT);
         $stmt->execute();
     }
     if ($winner === false) {
         // winner doesn't exist
         $sql = "INSERT INTO winners (name, place) VALUES(:name, :place)";
         $stmt = $db->prepare(':name', $_POST['winner']['name'], PDO::PARAM_STR);
         $stmt->prepare(':place', $place, PDO::PARAM_INT);
         $stmt->execute();
         $winner = $db->lastInsertId();
     }
     $round = new Round($db);
     $rows = $round->getRows();
     $sql = "INSERT INTO winners (player, ticket, date, round, leftToPay, price, status, row) " . "VALUES(:player, :ticket, NOW(), :round, :price, :price, 5, :row)";
     $stmt = $db->prepare($sql);
     $stmt->bindValue(":player", $winner['id'], PDO::PARAM_INT);
     $stmt->bindValue(":ticket", $_POST['winner']['verification'], PDO::PARAM_INT);
     $stmt->bindValue(":round", $round->getId(), PDO::PARAM_INT);
     $stmt->bindValue(":price", $_POST['winner']['price'], PDO::PARAM_INT);
     $stmt->bindValue(":row", $rows, PDO::PARAM_INT);
     $stmt->execute();
     return json_encode(['status' => true]);
 }