-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddProject.php
More file actions
executable file
·146 lines (124 loc) · 3.85 KB
/
addProject.php
File metadata and controls
executable file
·146 lines (124 loc) · 3.85 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
136
137
138
139
140
141
142
143
144
145
146
<?php
session_start();
ob_start();
$Ssn = $_SESSION["Ssn"];
$Lname = $_SESSION["Lname"];
// Include config file
require_once "config.php";
?>
<?php
// Define variables and initialize with empty values
$Pno = "";
$Pno_err = $Ssn_err = $Hours_err = "" ;
$SQL_err="";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate Project number
$Pno = trim($_POST["Pno"]);
if(empty($Pno)){
$Pno_err = "Please select a project.";
}
// Validate Hours
$Hours = trim($_POST["Hours"]);
if(empty($Hours)){
$Hours_err = "Please enter hours (1-40)";
}
// Validate the SSN
if(empty($Ssn)){
$Ssn_err = "No SSN.";
}
// Check input errors before inserting in database
if(empty($Ssn_err) && empty($Hours_err) && empty($Pno_err) ){
// Prepare an insert statement
$sql = "INSERT INTO WORKS_ON (Essn, Pno, Hours) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, 'sii', $param_Ssn, $param_Pno, $param_Hours);
// Set parameters
$param_Ssn = $Ssn;
$param_Pno = $Pno;
$param_Hours = $Hours;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records created successfully. Redirect to landing page
// header("location: index.php");
// exit();
} else{
// Error
echo "Error";
//exit();
$SQL_err = mysqli_error($link);
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Company DB</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-10">
<div class="page-header">
<h3>Add a Project </h3>
<h4><?php echo $Lname;?> SSN = <?php echo $Ssn;?></h4>
</div>
<?php
echo $SQL_err;
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if (!$conn) {
die('Could not connect: ' . mysqli_error());
}
$sql = "SELECT Pnumber, Pname FROM PROJECT";
$result = mysqli_query($conn, $sql);
if (!$result) {
die("Query to show fields from table failed");
}
$num_row = mysqli_num_rows($result);
?>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($Ssn_err)) ? 'has-error' : ''; ?>">
<label>Project number & name</label>
<select name="Pno" class="form-control">
<?php
for($i=0; $i<$num_row; $i++) {
$Pnos=mysqli_fetch_row($result);
echo "<option value='$Pnos[0]' >".$Pnos[0]." ".$Pnos[1]."</option>";
}
?>
</select>
<span class="help-block"><?php echo $Pno_err;?></span>
</div>
<div class="form-group <?php echo (!empty($Hours_err)) ? 'has-error' : ''; ?>">
<label>Hours </label>
<input type="number" name="Hours" class="form-control" min="1" max="80" value="">
<span class="help-block"><?php echo $Hours_err;?></span>
</div>
<div>
<input type="submit" class="btn btn-success pull-left" value="Add Project">
<a href="viewProjects.php" class="btn btn-primary">List Projects</a>
</div>
</form>
<?php
mysqli_free_result($result);
mysqli_close($conn);
?>
</body>
</html>