}
    ?>
	<br>
	<?php 
    //printf("%d insert player %s %s  ", $i, $player_i[0], $player_i[1]);
    $result = player::insert($player_i[0], $player_i[1], $player_i[2]);
    $player_id = $result->player_id;
    $first_name = $result->first_name;
    $last_name = $result->last_name;
    printf("insert Player " . "{$player_id}: " . "{$first_name}" . " and " . "{$last_name}" . " succeeded!");
    if (count($player_i) == 8) {
        ?>
	<br>
	<?php 
        //printf("%d insert Passing player %s %s ", $i, $player_i[6], $player_i[7]);
        $result = player::insert($player_i[6], $player_i[7], $player_i[2]);
        $player_id = $result->player_id;
        $first_name = $result->first_name;
        $last_name = $result->last_name;
        printf("insert Player " . "{$player_id}: " . "{$first_name}" . " and " . "{$last_name}" . " succeeded!");
    }
}
?>
<br>
<?php 
// $exist = $conn->query("select * from Player p where ");
// if($exist){
// 	$next_row = $exist->fetch_assoc();
// 	printf($next_row["first_name"]);
// 	printf(count($next_row));
// }
예제 #2
0
 public static function addEventToGame($game_id, $type, $first, $last, $score, $team)
 {
     $mysqli = new mysqli("classroom.cs.unc.edu", "hongkun", "CH@ngemenow99Please!hongkun", "hongkundb");
     /*check the connection*/
     if (mysqli_connect_errno()) {
         printf("Connection failed: %s\n", mysqli_connect_errno);
         exit;
     }
     // First: add the score to specific team, if team is found
     $teamSelected = $mysqli->query("select * from Game where (Team1 = '{$team}' || Team2 = '{$team}') && game_id = '{$game_id}'");
     if ($teamSelected->num_rows == 0) {
         header("HTTP/1.0 400 Bad Request");
         printMessageJSON("Bad Request, Team Not Match");
         exit;
     }
     $teamSelected = $teamSelected->fetch_row();
     $allAcore = [$teamSelected[1] => $teamSelected[3], $teamSelected[2] => $teamSelected[4]];
     if ($type == "fieldgoal") {
         $allAcore[$team] += 3;
     } else {
         if ($type == "passing") {
             $allAcore[$team] += 7;
         } else {
             if ($type == "rushing") {
                 $allAcore[$team] += 7;
             }
         }
     }
     // if the score is not match, exit();
     if ($allAcore[$team] != $score) {
         header("HTTP/1.0 400 Bad Request");
         printMessageJSON("Bad Request, Score Not Match");
         exit;
     }
     if ($allAcore[$teamSelected[1]] >= $allAcore[$teamSelected[2]]) {
         $winner = [$teamSelected[1] => $allAcore[$teamSelected[1]]];
         $loser = [$teamSelected[2] => $allAcore[$teamSelected[2]]];
     } else {
         $winner = [$teamSelected[2] => $allAcore[$teamSelected[2]]];
         $loser = [$teamSelected[1] => $allAcore[$teamSelected[1]]];
     }
     // Update the team score in Game Table.
     $gameUpdated = game::update($game_id, key($winner), key($loser), current($winner), current($loser));
     // Second: find the player in the team specified.
     // if it exists, return p_id, if not, insert it and return the p_id.
     $player_id = $mysqli->query("select player_id from Player where first_name = '{$first}' && last_name = '{$last}' && team_name = '{$team}'");
     if ($player_id->num_rows == 0) {
         $newPlayer = player::insert($first, $last, $team);
         $newPlayer = (array) $newPlayer;
         $player_id = intval($newPlayer["player_id"]);
     } else {
         $player_id = $player_id->fetch_row()[0];
     }
     // Use the p_id and game_id to find the event of the type,
     // if it exists, increase the number of the type
     // if not, insert the event.
     $eventSelected = $mysqli->query("select event_id from Event where p_id = '{$player_id}' && g_id = '{$game_id}'");
     $cnt = ["rushing" => 0, "passing" => 0, "fieldgoal" => 0, "passTo" => NULL];
     $cnt[$type]++;
     if ($eventSelected->num_rows == 0) {
         $event_id = event::insert($player_id, $game_id, $cnt["rushing"], $cnt["passing"], $cnt["fieldgoal"], $cnt["passTo"]);
     } else {
         $event_id = event::update($player_id, $game_id, $cnt["rushing"], $cnt["passing"], $cnt["fieldgoal"], $cnt["passTo"]);
     }
     // var_dump($event_id);
     $ids = [$game_id, $player_id];
     return $ids;
 }