-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdo.php
222 lines (212 loc) · 5.48 KB
/
pdo.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
$host = "localhost";
$db = "pdo";
$user1 = "root";
$pass = "";
try
{
$dsn = "mysql:host=" . $host . ";dbname=" . $db;
$con = new PDO($dsn, $user1, $pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch(PDOException $error)
{
echo "Connection Failed " . $error->getMessage();
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" >
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_REQUEST['search']))
{
$id = $_REQUEST['id'];
$searchQuery = "SELECT * FROM user WHERE id= :id";
$searchQueryRun = $con->prepare($searchQuery);
$searchResultExec = $searchQueryRun->execute(array(
":id" => $id
));
if ($searchResultExec)
{
if ($searchQueryRun->rowCount() > 0)
{
foreach ($searchQueryRun as $row)
{
$id = $row->id;
$user = $row->user;
$pass = $row->pass;
}
}
else
{
$error = 'No Id Found';
}
}
else
{
$error = 'No Search is not Run';
}
}
$user = "";
//INSERT
function filter($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_REQUEST['insert']))
{
$user = filter($_REQUEST['user']);
$pass = filter($_REQUEST['pass']);
if (!(empty($user) && empty($pass)))
{
if (preg_match("/^[a-zA-Z ]*$/", $user))
{
$insertQuery = "INSERT INTO user(user,pass) VALUES(:u,:p)";
$insertQueryRun = $con->prepare($insertQuery);
$InsertResult = $insertQueryRun->execute(array(
':u' => $user,
':p' => $pass
));
if ($InsertResult)
{
header("location:single.php");
}
}
else
{
$error = "Name Must be Character";
}
}
else
{
$error = "All fields blank";
}
}
//Update
if (isset($_REQUEST['update']))
{
$id = filter($_REQUEST['id']);
$user = filter($_REQUEST['user']);
$pass = filter($_REQUEST['pass']);
if (!empty($id))
{
if (!(empty($user) || empty($pass)))
{
if (preg_match("/^[a-zA-Z ]*$/", $user))
{
$updateQuery = "UPDATE user SET user=:u,pass=:p where id=:id";
$updateQueryRun = $con->prepare($updateQuery);
$updateResult = $updateQueryRun->execute(array(
':u' => $user,
':p' => $pass,
':id' => $id
));
}
else
{
$error = "User Should be in String";
}
}
else
{
$error = "All field Required";
}
}
else
{
$error = "Choose ID for Update the field";
}
}
//delete
if (isset($_REQUEST['delete']))
{
$id = $_REQUEST['id'];
if (!empty($id))
{
$deleteQuery = "DELETE FROM user where id=:id";
$deleteQueryRun = $con->prepare($deleteQuery);
$deleteResult = $deleteQueryRun->execute(array(
':id' => $id
));
}
else
{
$error = "Please enter ID for Delete the Data";
}
}
?>
<div class="container col-6 border border-muted pt-5 mt-5">
<center class="display-4">CRUD PDO</center>
<div class="alert alert-danger"><?php if (isset($error))
{
echo $error;
} ?></div>
<form action="" method="">
<div class="form-group clearfix">
<label>Enter Your ID For Edit :</label>
<input type="text" name="id" class="form-control float-right"
value="<?php if (isset($_REQUEST['search']))
{
echo $id;
} ?>" placeholder="ID" maxlength="2" style="width:70%">
</div>
<div class="form-group">
<input type="text" name="user" value="<?php echo $user ?>" class="form-control" placeholder="name">
</div>
<div class="form-group">
<input type="text" name="pass" value="<?php echo $pass ?>" class="form-control" placeholder="pass">
</div>
<div class="form-group">
<button type="submit" name="insert" class="btn btn-outline-success">Insert</button>
<button type="submit" name="display" class="btn btn-outline-info">Display</button>
<button type="submit" name="search" class="btn btn-outline-primary">Search</button>
<button type="submit" name="update" class="btn btn-outline-primary">Update</button>
<button type="submit" name="delete" class="btn btn-outline-danger">Delete</button>
</div>
</form>
<?php
//#############Display on Table#############//
if (isset($_REQUEST["display"]))
{
$fetchQuery = "SELECT * FROM user";
$fetchQueryRun = $con->query($fetchQuery);
?>
<table class="table">
<thead>
<tr>
<th>S.NO</th>
<th>NAME</th>
<th>PASS</th>
</tr>
</thead>
<tbody>
<?php while ($row = $fetchQueryRun->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['user'] ?></td>
<td><?php echo $row['pass'] ?></td>
</tr>
</tr>
<?php
endwhile; ?>
</tbody>
</table>
<?php
}
?>
</div>
</body>
</html>