-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDependents.php
More file actions
executable file
·135 lines (118 loc) · 5.1 KB
/
Copy pathcreateDependents.php
File metadata and controls
executable file
·135 lines (118 loc) · 5.1 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
session_start();
$Ssn = $_SESSION["Ssn"];
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$Dname = $Relationship = $Bdate = $Sex ="" ;
$Dname_err = $Relationship_err = $Sex_err =$Bdate_err= "" ;
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate Dependent name
$Dname = trim($_POST["Dname"]);
if(empty($Dname)){
$Dname_err = "Please enter a Dname.";
} elseif(!filter_var($Dname, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$Dname_err = "Please enter a valid name.";
}
// Validate Relationship
$Relationship = trim($_POST["Relationship"]);
if(empty($Relationship)){
$Relationship_err = "Please enter a Relationship.";
} elseif(!filter_var($Relationship, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$Relationship_err = "Please enter a valid Relationship.";
}
// Validate Sex
$Sex = trim($_POST["Sex"]);
if(empty($Sex)){
$Sex_err = "Please enter Sex.";
}
// Validate Birthdate
$Bdate = trim($_POST["Bdate"]);
if(empty($Bdate)){
$Bdate_err = "Please enter birthdate.";
}
// Check input errors before inserting in database
if(empty($Dname_err) && empty($Relationship_err) && empty($Sex_err) && empty($Bdate_err)){
// Prepare an insert statement
$sql = "INSERT INTO DEPENDENT (Essn, Dependent_name, Sex, Bdate, Relationship)
VALUES (?, ?, ?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssss", $param_Ssn, $param_Dname, $param_Sex,
$param_Bdate, $param_Relationship);
// Set parameters
$param_Ssn = $Ssn;
$param_Dname = $Dname;
$param_Sex = $Sex;
$param_Bdate = $Bdate;
$param_Relationship = $Relationship;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "<center><h4>Error while creating new dependent</h4></center>";
$Dname_err = "Re-enter all values";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Create Dependent</h2>
<h3> For employee with SSN = <?php echo $Ssn; ?> </h3>
</div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($Dname_err)) ? 'has-error' : ''; ?>">
<label>Dependent's Name</label>
<input type="text" name="Dname" class="form-control" value="<?php echo $Dname; ?>">
<span class="help-block"><?php echo $Dname_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Relationship_err)) ? 'has-error' : ''; ?>">
<label>Relationship</label>
<input type="text" name="Relationship" class="form-control" value="<?php echo $Relationship; ?>">
<span class="help-block"><?php echo $Relationship_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Sex_err)) ? 'has-error' : ''; ?>">
<label>Sex</label>
<input type="text" name="Sex" class="form-control" value="<?php echo $Sex; ?>">
<span class="help-block"><?php echo $Sex_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Bdate_err)) ? 'has-error' : ''; ?>">
<label>Birth date</label>
<input type="date" name="Bdate" class="form-control" value="<?php echo date('Y-m-d'); ?>">
<span class="help-block"><?php echo $Bdate_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>