-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_item.php
155 lines (129 loc) · 3.54 KB
/
add_item.php
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
148
149
150
151
152
153
154
155
<?php
session_start();
if(!isset($_SESSION["username"])){
header("location: login.php");
exit;
}
$description = "";
$umsr = "";
$category = "";
$qty = "";
$price = "";
if(isset($_POST["save"]))
{
$description = trim($_POST["description"]);
$umsr = $_POST["umsr"];
$category = $_POST["category"];
$qty = trim($_POST["qtyonhand"]);
$price = trim($_POST["price"]);
//errorTrapping($qty,$umsr,$price);
if(errorTrapping($qty,$umsr,$price) == false){
$con = mysqli_connect("localhost", "root", "", "inventory");
if($con)
{
$sql = "insert into item (description, umsr, cid, qtyonhand, price)
values ('".$description."', '".$umsr."', ".$category.", ".$qty.", ".$price.") ";
mysqli_query($con, $sql);
echo "<p>Item was saved successfully...</p>";
}
else
{
echo "<p>Error connecting to DB...</p>";
}
mysqli_close($con);
$description = "";
$umsr = "";
$category = "";
$qty = "";
$price = "";
}
}
function errorTrapping($qty,$umsr,$price){
$nonDecimalUmsr = ["pc","set","doz"];
$isError = false;
if(is_numeric($qty) === FALSE){
echo "<p> Quantity on hand should be a number </p>";
$isError = true;
}
if(is_numeric($price) === FALSE){
echo "<p> price should be a number </p>";
$isError = true;
}
if($qty < 0 || $price <0){
echo "<p> Quantity on hand or price must not be a negative number </p>";
$isError = true;
}
if((array_search($umsr,$nonDecimalUmsr)) !== FALSE){
if ((fmod($qty,1) !== 0.00)){
echo "<p> Quantity on hand can only be decimal for Kilo</p>";
$isError = true;
}
}
return $isError;
}
?>
</script>
<html>
<head>
<title>Add Item</title>
</head>
<body>
<form method="POST" action="add_item.php">
<table>
<tr>
<td>Description:</td>
<td><input type="text" name="description" required value ="<?php echo "$description";?>"/></td>
</tr>
<tr>
<td>Unit of Measure:</td>
<td>
<select name="umsr">
<option value="pc" <?php if($umsr == "pc") echo "selected"; ?>>pc</option>
<option value="set"<?php if($umsr == "set") echo "selected"; ?>>set</option>
<option value="kl"<?php if($umsr == "kl") echo "selected"; ?>>kilo</option>
<option value="doz"<?php if($umsr == "doz") echo "selected"; ?>>dozen</option>
</select>
</td>
</tr>
<tr>
<td>Category:</td>
<td>
<?php
$con = mysqli_connect("localhost", "root", "", "inventory");
if($con)
{
$categories = mysqli_query($con, "select * from category order by description");
if(mysqli_num_rows($categories) > 0)
{
echo "<select name='category'>";
while($row = mysqli_fetch_row($categories))
{
echo "<option value='".$row[0]."' <?php if($category == '".$row[0]."') {echo 'selected';} ?>".$row[1]."</option>";
}
echo "</select>";
}
}
else
{
echo "<p>Error DB Connection</p>";
}
?>
</td>
</tr>
<tr>
<td>Quantity on Hand:</td>
<td><input type="text" name="qtyonhand" required value ="<?php echo "$qty";?>"/></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" required value ="<?php echo "$price";?>"/></td>
</tr>
<tr>
<td></td>
<td><button type="submit" name="save">Save</button></td>
</tr>
</table>
</form>
<a href = "./list_all.php">View List</a>
</body>
</html>