-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_input_daily.php
66 lines (60 loc) · 2.15 KB
/
p_input_daily.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
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: index.php');
exit;
}
if ($_SESSION['type'] != 'player') {
header('Location: index.php');
exit;
}
require 'db/db_connect.php';
$conn = $con;
$userid = $_SESSION['player_id'];
$coach_id = $_SESSION['coach_id'];
//Get current
if ($_POST['shotsmade'] > $_POST['shotstaken']) {
header("Location: error.php?a=Shots made cannot me greater than shots taken!&b=home.php");
exit();
}
//Get master Goal
$stmt = $conn->prepare('SELECT goal FROM coaches WHERE coach_id = ?');
$stmt->bind_param('i', $coach_id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$master_goal = $row['goal'];
$sql = 'SELECT shots_made, shots_taken FROM shots WHERE player_id = ? AND shot_date = CURDATE()';
$getshots = $conn->prepare($sql);
$getshots->bind_param('i', $userid);
$getshots->execute();
$gotshots = $getshots->get_result();
$shots = $gotshots->fetch_assoc();
$today_shots_made = $shots['shots_made'];
$today_shots_taken = $shots['shots_taken'];
$added_taken = $today_shots_taken + $_POST['shotstaken'];
$added_made = $today_shots_made + $_POST['shotsmade'];
// Insert or update the shots data for the user
if (!is_null($today_shots_made)){ //DUP
$query = "UPDATE shots SET shots_taken = ?, shots_made = ?
WHERE player_id = ? AND shot_date = CURDATE()";
$stmt = $conn->prepare($query);
$stmt->bind_param("iii", $added_taken, $added_made, $userid);
$stmt->execute();
$stmt->close();
}
if (is_null($today_shots_made)){ //NEW
$query = "INSERT INTO shots (player_id, shot_date, shots_taken, shots_made)
VALUES (?, CURDATE(), ?, ?)";
$stmt = $conn->prepare($query);
$stmt->bind_param("iii", $userid, $_POST['shotstaken'], $_POST['shotsmade']);
$stmt->execute();
$query2 = "UPDATE shots SET goal = ? WHERE player_id = ? AND shot_date = CURDATE()";
$stmt = $conn->prepare($query2);
$stmt->bind_param("ii", $master_goal, $userid);
$stmt->execute();
$stmt->close();
}
header('Location: player_dashboard.php');