-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweightTrackerAdd.php
More file actions
65 lines (52 loc) · 1.88 KB
/
weightTrackerAdd.php
File metadata and controls
65 lines (52 loc) · 1.88 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
<?php
//workoutTrackerInsert.php
// Include config file
require_once 'config.php';
// Define variables and initialize with empty values
$user_id = $weight = "";
$user_id_err = $weight_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 weight
if(empty(trim($_POST['weight']))){
$weight_err = "Please enter a weight.";
}else{
$weight = trim($_POST['weight']);
}
// Check input errors before inserting in database
if(empty($user_id_err) && empty($weight_err)){
// Prepare an insert statement
$sql = "INSERT INTO weight_tracker (user_id, weight) VALUES (?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_user_id, $param_weight);
// Set parameters
$param_user_id = $user_id;
$param_weight = $weight;
// 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($weight_err))){
header("location: indexTest.html?p_err=".$weight_err); // change the html to the one you need to show
}
}
?>