-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddplayer.php
120 lines (104 loc) · 3.6 KB
/
addplayer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<html>
<head>
<title>Add Player</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
//$data = array("name", "position", "type", "team", "year");
$data_missing = array();
foreach($_POST as $key => $value) {
if(empty($value)) {
$data_missing[] = $key;
}
$$key = $value;
}
// $name, $position, $type, $team, $year // are set now
if(empty($data_missing)){
require_once('../../mysqli_connect.php');
// Check primary keys for if this row already exists
if(mysqli_fetch_array(mysqli_query($dbc, "SELECT name
FROM players
WHERE name = '" . $name . "'"))['name'] == $name) {
// Player already exists
echo 'This player already exists';
} else {
// Player does not exist
// Add player to players
$query = "INSERT INTO players VALUES (?, ?)";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "ss", $name, $position);
mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);
// Add player to specific type
$stmt = mysqli_prepare($dbc, "INSERT INTO " . $type . " VALUES (?, ?)");
if ($type == "active" || $type == "free") {
mysqli_stmt_bind_param($stmt, "ss", $name, $team);
} else { // retired
mysqli_stmt_bind_param($stmt, "si", $name, $year);
}
mysqli_stmt_execute($stmt);
$affected_rows += mysqli_stmt_affected_rows($stmt);
if ($affected_rows == 2){
echo 'Player Entered';
} else {
echo 'Unknown Error Occurred<br />';
//echo mysqli_error();
}
//mysqli_stmt_close($stmt);
//mysqli_close($dbc);
}
} else {
echo 'You need to enter the following data<br />';
foreach($data_missing as $missing){
echo "$missing<br />";
}
}
}
?>
<form action="" method="post">
<b>Add a New Player</b><br>
Name:<br>
<input type="text" name="name"><br>
Position:<br>
<input type="text" name="position"><br>
Status:<br>
<input type="radio" name="type" value="active" onclick="showTeam()" checked>Active<br>
<input type="radio" name="type" value="retired" onclick="showYear()">Retired<br>
<input type="radio" name="type" value="free_agent" onclick="showTeam()">Free Agent<br>
<div id="team">
Team / Last Team:<br>
<select name="team">
<?php
require_once('../../mysqli_connect.php');
$query = "SELECT city, team_name
FROM team
ORDER BY city";
$response = mysqli_query($dbc, $query);
if($response) {
while($row = mysqli_fetch_array($response)) {
echo "<option>" . $row['team_name'] . "</option>";
}
} ?>
</select>
</div>
<div id="year" style="display:none">
Retirement Year:<br>
<select name="year">
<?php for ($i = 2018; $i >= 1917; $i--) {echo '<option>', $i, '</option>';} ?>
</select><br>
</div>
<input type="submit" name="submit" value="Submit">
</form>
<script>
function showTeam() {
document.getElementById("team").style.display = "";
document.getElementById("year").style.display = "none";
}
function showYear() {
document.getElementById("year").style.display = "";
document.getElementById("team").style.display = "none";
}
</script>
</body>
</html>