-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddPlayer.php
91 lines (79 loc) · 1.82 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
<?php
require_once "pdo.php";
session_start();
if ( ! isset($_SESSION['name']) )
{
die('Not logged in');
}
//
// This checks to make sure required variables are set and if they it then checks
// to make sure there is valid values.
// If all that goes through then it will execute a sql statement to add a new entry
//
if ( isset($_POST['player_name']))
{
if((strlen($_POST['player_name']) > 1))
{
$sql = "INSERT INTO players (player_name, goals, assists)
VALUES (:player_name, :goals, :assists)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':player_name' => $_POST['player_name'],
':goals' => $_POST['goals'],
':assists' => $_POST['assists']));
header('Location: index.php');
return;
}
}
//
// Redirects to index.php is cancel is selected
//
if(isset($_POST['cancel']))
{
header('Location: index.php');
return;
}
//
// Sends an error is the required variables have invalid values (empty)
//
if ( isset($_POST['player_name']) )
{
if(strlen($_POST['player_name']) < 1)
{
$_SESSION['error'] = "Player name is required";
header("Location: addPlayer.php");
return;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Beer League Database</title>
</head>
<body>
<h1>
Beer League Database
</h1>
<?php
if ( isset($_SESSION['error']))
{
if ( isset($_SESSION['error']) )
{
echo('<p style="color: red;">'.htmlentities($_SESSION['error'])."</p>\n");
unset($_SESSION['error']);
}
}
?>
<form method="post" id="addForm">
<p>Player Name:
<input type="text" name="player_name" size="60"/></p>
<p>Goals:
<input type="number" step=1 name="goals" size="60"/></p>
<p>Assists:
<input type="number" step=1 name="assists"/></p>
</form>
<input type="submit" value="Add" form="addForm">
<input type="submit" name="cancel" value="Cancel" form="addForm">
</body>
</html>