-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertingredient.php
More file actions
147 lines (129 loc) · 4.47 KB
/
insertingredient.php
File metadata and controls
147 lines (129 loc) · 4.47 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
147
<?php
include "includes/base.php";
?>
<html>
<head>
<title>Insert Ingredient</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<?php include("includes/header.php");?>
<?//php include("includes/nav.php");?>
<div id='section'>
<br>
<div class="container">
<form class='form-horizontal' action="" method="POST">
<fieldset>
<legend>Insert into Ingredient Inventory</legend>
<?php
if (isset($_SESSION['error']) || isset($_SESSION['success'])) {
if ($_SESSION['error']) {
echo "<div class='btn btn-danger col-md-4 form-control'>". $_SESSION['error']. "</div>";
echo "<br><br>";
$_SESSION['error'] = '';
}
else if($_SESSION['success']) {
print "<div class='btn btn-success form-control'>". $_SESSION['success']. "</div>";
echo "<br><br>";
$_SESSION['success'] = '';
}
}
?>
<div class="form-group">
<label class="col-md-4 control-label">Name:</label>
<div class="col-md-4">
<input class='form-control input-md' type="text" name="name" value="<?php echo $_POST['name']?>">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Inventory:</label>
<div class="col-md-4">
<input class="form-control input-md" type="text" name="inventory" value="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Lot Number:</label>
<div class="col-md-4">
<input class="form-control input-md" type="text" name="lot_number" value="">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Unit ID:</label>
<div class="col-md-4">
<input class="form-control input-md" type="text" name="unit_id" value="">
</div>
</div>
<div class="form-group">
<label class='col-md-4 control-label'></label>
<div class='col-md-4'>
<input class="btn btn-info form-control" type="submit" name="submitnew_ind" value="Submit">
</div>
</div>
</fieldset>
</form>
</div>
<div class="container">
<a href="inventory.php" class="col-md-4 btn-btn info">Back to Inventory</a><br>
</div>
</div>
<?php include("includes/footer.php");?>
</body>
</html>
<?php
if(isset($_POST['submitnew_ind'])) {
$name = htmlspecialchars($_POST['name']);
$inventory = htmlspecialchars($_POST['inventory']);
$unitid = htmlspecialchars($_POST['unit_id']);
$lotnumber = htmlspecialchars($_POST['lot_number']);
if($name == '' || $inventory == '' || $unitid == '' || $lotnumber == '') {
$_SESSION['error'] = "Error: All fields must be filled.";
//require "insertingredient.php";
header('Refresh:0');
exit();
}
else if(!is_numeric($inventory)) {
$_SESSION['error'] = "Error: Inventory must be a number.";
//require "insertingredient.php";
header('Refresh:0');
exit();
}
else if(!is_numeric($unitid)){
$_SESSION['error'] = "Error: Unit ID must be a number.";
//require "insertingredient.php";
header('Refresh:0');
exit();
}
$query = "SELECT unit_id FROM unit WHERE unit_id = ". $unitid;
if($stmt = mysqli_prepare($link, $query)){
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 0) {
$_SESSION['error'] = "Error: That unit ID doesn't exist.";
mysqli_stmt_close($stmt);
//require "insertingredient.php";
header('Refresh:0');
exit();
}
mysqli_stmt_close($stmt);
}
$sql = "INSERT INTO ingredient (name,inventory,lot_number,unit_id) VALUES (?,?,?,?);";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt,"ssss",$name,$inventory,$lotnumber,$unitid);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
$_SESSION['success'] = "Success: Ingredient has been added.";
//require "insertingredient.php";
header('Refresh:0');
exit();
}
else {
$_SESSION['error'] = "Error: Ingredient not successfully added.";
//require "insertingredient.php";
header('Refresh:0');
exit();
}
}
?>
<?php
mysqli_close($link);
?>