-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkoutPlannerAdd.php
More file actions
83 lines (70 loc) · 2.76 KB
/
workoutPlannerAdd.php
File metadata and controls
83 lines (70 loc) · 2.76 KB
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
<?php
//workoutTrackerInsert.php
// Include config file
require_once 'config.php';
// Define variables and initialize with empty values
$user_id = $workout_id = $amount = $plan_id = "";
$user_id_err = $workout_id_err = $amount_err = $plan_id_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate username
if(empty(trim($_POST["user_id"]))){
$user_id_err = "Please enter a user_id.";
}
else{
$user_id = trim($_POST['user_id']);
}
// Validate workout_id
if(empty(trim($_POST['workout_id']))){
$workout_id_err = "Please enter a workout_id.";
}else{
$workout_id = trim($_POST['workout_id']);
}
// Validate plan_id
if(empty(trim($_POST['plan_id']))){
$plan_id_err = "Please enter a plan_id.";
}else{
$plan_id = trim($_POST['plan_id']);
}
// Validate amount
if(empty(trim($_POST["amount"]))){
$amount_err = 'Please enter amount.';
} else{
$amount = trim($_POST['amount']);
}
// Check input errors before inserting in database
if(empty($user_id_err) && empty($workout_id_err) && empty($amount_err) && empty($plan_id_err)){
// Prepare an insert statement
$sql = "INSERT INTO workout_plan (user_id, workout_id, plan_id, plan_amount) VALUES (?, ?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssss", $param_user_id, $param_workout_id, $param_plan_id, $param_amount);
// Set parameters
$param_user_id = $user_id;
$param_workout_id = $workout_id;
$param_plan_id = $plan_id;
$param_amount = $amount;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: indexTest.html");
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
if(!(empty($user_id_err))){
header("location: indexTest.html?u_err=".$user_id_err); // send error message in the err variable
}else if(!(empty($workout_id_err))){
header("location: indexTest.html?p_err=".$workout_id_err); // change the html to the one you need to show
}else if(!(empty($plan_id_err))){
header("location: indexTest.html?cp_err=".$plan_id_err); // change the html to the one you need to show
}else if(!(empty($amount_err))){
header("location: indexTest.html?cp_err=".$amount_err); // change the html to the one you need to show
}
}
?>