-
Notifications
You must be signed in to change notification settings - Fork 1
/
Post.php
158 lines (139 loc) · 3.99 KB
/
Post.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
156
157
158
<!DOCTYPE html>
<html lang = "en">
<head>
<title> Form </title>
<meta charset = "utf-8" />
<link rel="stylesheet" type="text/css" href="layout.css" />
</head>
<body>
<?php
include ('navbar.php');
include('kozycorner.dbconfig.inc');
// Connect to MySQL
$conn = mysqli_connect($hostname,$username, $password, $dbname);
if (mysqli_connect_errno()) {
print "Connect failed: " . mysqli_connect_error();
exit;
}
//Set variables
$section = $_POST['section'];
$pname = $_POST['petname'];
$ptype = $_POST['type'];
$breed = $_POST['breed'];
$color = $_POST['color'];
$weight = $_POST['weight'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zipcode'];
$date = $_POST['date'];
$mixb = $_POST['mixbreed'];
$description = $_POST['description'];
if (isSet($_POST['branch'])) {
$branch = $_POST['branch'];
} else {
$branch = "na";
}
if($pname == null){
$pname = '';
}
if($weight == null){
$weight = '';
}
if($address == null){
$address = '';
}
if(strcmp($branch, "na") == 0){
$branch = 1;
}else if(strcmp($branch, "la") == 0){
$branch = 2;
}else if(strcmp($branch, "atl") == 0){
$branch = 3;
}else if(strcmp($branch, "nyc") == 0){
$branch = 4;
}else if(strcmp($branch, "dal") == 0){
$branch = 5;
}else{
$branch = 6;
}
if(strcmp($mixb, "TRUE") == 0){
$mixb = TRUE;
}else{
$mixb = FALSE;
}
//Change all words so only first letter is upper case
$pname = ucfirst($pname);
$ptype = ucfirst($ptype);
$breed = ucfirst($breed);
$color = ucfirst($color);
$address = ucfirst($address);
$city = ucfirst($city);
$state = strtoupper($state);
//Selects and runs to hold breed table data
$query = "SELECT * FROM breed";
$result = mysqli_query($conn, $query);
if (!$result) {
print "Error - the breed search query could not be executed: " . mysqli_error($conn);
exit;
}
$num_rows = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
//First traversal to see if breed exist
$exist = FALSE;
for($row_num = 0; $row_num < $num_rows; $row_num++) {
$str1 = $row['breed_name'];
if(strcmp($str1, $breed) == 0){
$breed = $row['breed_id'];
$exist = TRUE;
}
// Move to the next row
$row = mysqli_fetch_assoc($result);
}
//If breed does not exist then create breed and set value and traverse again
if($exist == FALSE){
//Adds new breed to breed table
$query = "INSERT INTO $dbname.breed(breed_name) VALUES('$breed')";
$result = mysqli_query($conn, $query);
if (!$result) {
print "Error - the breed query could not be executed: " . mysqli_error($conn);
exit;
}
$query = "SELECT * FROM breed";
// Run query
$result = mysqli_query($conn, $query);
if (!$result) {
print "Error - the breed search query could not be executed: " . mysqli_error($conn);
exit;
}
//Selects and runs to hold breed table data
$num_rows = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$exist = FALSE;
for($row_num = 0; $row_num < $num_rows; $row_num++) {
$str1 = $row['breed_name'];
if(strcmp($str1, $breed) == 0){
$breed = $row['breed_id'];
$exist = TRUE;
}
$row = mysqli_fetch_assoc($result);
}
}
//Inserts new post
$query = "INSERT INTO $dbname.pets (user_id, branch_id, breed_id, section, pet_name, pet_type, color, weight, gender, address, city, state, zipcode, date_found, is_mixed, pet_description)
VALUES('$_COOKIE[user_id]', '$branch', '$breed', '$section', '$pname', '$ptype', '$color', '$weight', '$gender', '$address', '$city', '$state', '$zip', '$date', '$mixb', '$description')";
$result = mysqli_query($conn, $query);
if (!$result) {
print "Error - the insert query could not be executed: " . mysqli_error($conn);
exit;
}
?>
<div class="wrapper">
<h1>You post has successfully been submitted!</h1>
</div>
<?php
include ('footer.php');
?>
</body>
</head>
</html>