-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin_add.php
137 lines (127 loc) · 4.04 KB
/
admin_add.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
<?php
session_start();
if((!isset($_SESSION['manager']) && !isset($_SESSION['expert'])))
{
header("Location:index.php");
}
$title = "Add new Medicine";
require "./template/header.php";
require "./functions/database_functions.php";
$conn = db_connect();
if(isset($_POST['add']))
{
$serial = trim($_POST['serial']);
$serial = mysqli_real_escape_string($conn, $serial);
$title = trim($_POST['title']);
$title = mysqli_real_escape_string($conn, $title);
$manufacturer = trim($_POST['manufacturer']);
$manufacturer = mysqli_real_escape_string($conn, $manufacturer);
$descr = trim($_POST['descr']);
$descr = mysqli_real_escape_string($conn, $descr);
$price = floatval(trim($_POST['price']));
$price = mysqli_real_escape_string($conn, $price);
$use = trim($_POST['use_for']);
$use = mysqli_real_escape_string($conn, $use);
$type = trim($_POST['type']);
$type = mysqli_real_escape_string($conn, $type);
if(isset($_FILES['image']) && $_FILES['image']['name'] != "")
{
$image = $_FILES['image']['name'];
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$uploadDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . "bootstrap/img/";
$uploadDirectory .= $image;
move_uploaded_file($_FILES['image']['tmp_name'], $uploadDirectory);
}
//add in one use, if no use create one in db
$finduse = "SELECT * FROM used_for WHERE used_for_name = '$use'";
$findResult = mysqli_query($conn, $finduse);
if(mysqli_num_rows($findResult)==0)
{
$insertuse = "INSERT INTO used_for(used_for_name) VALUES ('$use')";
$insertResult = mysqli_query($conn, $insertuse);
if(!$insertResult)
{
echo "Can't add new Uses " . mysqli_error($conn);
exit;
}
$used_for_id = mysqli_insert_id($conn);
}
else
{
$row = mysqli_fetch_assoc($findResult);
$used_for_id = $row['used_for_id'];
}
//add in one type, if no type create one in db
$findtype = "SELECT * FROM type WHERE type_name = '$type'";
$findResult = mysqli_query($conn, $findtype);
if(mysqli_num_rows($findResult)==0)
{
$inserttype = "INSERT INTO type(type_name) VALUES ('$type')";
$insertResult = mysqli_query($conn, $inserttype);
if(!$insertResult){
echo "Can't add new Type " . mysqli_error($conn);
exit;
}
$type_id = mysqli_insert_id($conn);
}
else
{
$row = mysqli_fetch_assoc($findResult);
$type_id = $row['type_id'];
}
//data in medicines table
$query = "INSERT INTO medicines VALUES ('" . $serial . "', '" . $title . "', '" . $manufacturer . "', '" . $image . "', '" . $descr . "', '" . $price . "', '" . $used_for_id . "', '" . $type_id . "')";
$result = mysqli_query($conn, $query);
if(!$result)
{
echo "Can't add new data " . mysqli_error($conn);
exit;
}
else
{
header("Location: admin_med.php");
}
}
?>
<form method="post" action="admin_add.php" enctype="multipart/form-data">
<table class="table">
<tr>
<th>Serial no</th>
<td><input type="text" name="serial"></td>
</tr>
<tr>
<th>Medicine name</th>
<td><input type="text" name="title" required></td>
</tr>
<tr>
<th>Manufacturer</th>
<td><input type="text" name="manufacturer" required></td>
</tr>
<tr>
<th>Image</th>
<td><input type="file" name="image"></td>
</tr>
<tr>
<th>Description</th>
<td><textarea name="descr" cols="40" rows="5"></textarea></td>
</tr>
<tr>
<th>Price</th>
<td><input type="text" name="price" required></td>
</tr>
<th>Used for</th>
<td><input type="text" name="use_for" required></td>
</tr>
<tr>
<th>Types</th>
<td><input type="text" name="type" required></td>
</tr>
</table>
<input type="submit" name="add" value="Add new Medicine" class="btn btn-primary">
<a href="admin_med.php" class="btn btn-default">Cancel</a>
</form>
<br/>
<?php
if(isset($conn)) {mysqli_close($conn);}
require_once "./template/footer.php";
?>