-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
190 lines (165 loc) · 5.62 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="main.css" rel="stylesheet" />
<title>Company App</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "marioskyr";
$nameEdit='';
$addressEdit='';
$cityEdit='';
$pCodeEdit='';
$pNumEdit='';
$conn = new mysqli($servername, $username, $password, $db);
$connPDO = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
$connPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!empty($_GET['edit'])) {
$buttonName='Update';
$buttonCheck='updateBtn';
$editLocate = mysqli_real_escape_string($conn, $_GET['edit']);
$editData = "SELECT * FROM customersappdb WHERE id='$editLocate'";
$result = $conn->query($editData);
while($row = $result->fetch_assoc()) {
$nameEdit = $row["names"];
$addressEdit = $row["addresses"];
$cityEdit = $row["cities"];
$pCodeEdit = $row["pCodes"];
$pNumEdit = $row["pNumbers"];
}
if(isset($_POST['updateBtn'])) {
$name = $_POST["fname"];
$address = $_POST["address"];
$city = $_POST["city"];
$pCode = $_POST["pCode"];
$pNumber = $_POST["pNumber"];
$updateData = "UPDATE customersappdb SET names=?, addresses=?, cities=?, pCodes=?, pNumbers=? WHERE id=?";
$stmt = $connPDO->prepare($updateData);
$stmt->execute([$name, $address, $city, $pCode, $pNumber, $editLocate]);
header("location:/");
}
$conn->close();
} else {
$buttonName="Add";
$buttonCheck='submitBtn';
}
?>
<form method="POST" id="formInfo">
<!-- Name -->
<input
type="text"
class="inputCommon"
id="fnameInput"
name="fname"
value="<?php echo $nameEdit ?>"
placeholder="Name"
/>
<!-- Address -->
<input
type="text"
class="inputCommon"
id="addressInput"
name="address"
value="<?php echo $addressEdit ?>"
placeholder="Address"
/>
<!-- Container -->
<div class="pCodeCity">
<!-- City -->
<input
type="text"
class="inputCommon"
id="cityInput"
name="city"
value="<?php echo $cityEdit ?>"
placeholder="City"
/>
<!-- Postal Code -->
<input
type="text"
class="inputCommon"
id="pCodeInput"
name="pCode"
value="<?php echo $pCodeEdit ?>"
placeholder="Postal Code"
/>
</div>
<!-- Phone Number -->
<input
type="number"
class="inputCommon"
id="pNumberInput"
name="pNumber"
value="<?php echo $pNumEdit ?>"
placeholder="Phone Number"
/>
<!-- Submit Button -->
<input type="submit" id="submitBtn" name="<?php echo $buttonCheck; ?>" value="<?php echo $buttonName; ?>" />
</form>
<center>
<table style="width:80vw; margin-top: 40px;">
<tr>
<th style="border: 1px solid black;">Name</th>
<th style="border: 1px solid black;">Address</th>
<th style="border: 1px solid black;">City</th>
<th style="border: 1px solid black;">Postal Code</th>
<th style="border: 1px solid black;">Phone Number</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "marioskyr";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if(isset($_POST["submitBtn"])) {
$name = $_POST["fname"];
$address = $_POST["address"];
$city = $_POST["city"];
$pCode = $_POST["pCode"];
$pNumber = $_POST["pNumber"];
$stmt = $conn->prepare("INSERT INTO customersappdb (names, addresses, cities, pCodes, pNumbers) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param('sssii', $name, $address, $city, $pCode, $pNumber);
$stmt->execute();
$stmt->close();
$conn->close();
header("location:/");
}
$result = mysqli_query($conn,"SELECT * FROM customersappdb");
while($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td style="border: 1px solid black; padding: 10px 5px;">' . $row['names'] . '</td>';
echo '<td style="border: 1px solid black; padding: 10px 5px;">' . $row['addresses'] . "</td>";
echo '<td style="border: 1px solid black; padding: 10px 5px;">' . $row['cities'] . "</td>";
echo '<td style="border: 1px solid black; padding: 10px 5px;">' . $row['pCodes'] . "</td>";
echo '<td style="border: 1px solid black; padding: 10px 5px;">' . $row['pNumbers'] . "</td>";
echo '<td><a href="?edit='.$row['id'].'" class="fas fa-pencil-alt edit" name="edit"></a><a href="?delete='.$row['id'].'" class="far fa-times-circle delete" name="delete"></a></td>';
echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</body>';
echo '</html>';
if(!empty($_GET['delete'])) {
$idLocate = mysqli_real_escape_string($conn, $_GET['delete']);
$deleteID = "DELETE FROM customersappdb WHERE id='$idLocate'";
if($conn->query($deleteID) === TRUE) {
echo "<script>console.log('deleted succesfully')</script>";
header("location:/");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
}
?>