1
+ <?php
2
+ // create variable for connection
3
+ $ dsn = "mysql:host=localhost; dbname=test_db " ;
4
+ $ db_user = "root " ;
5
+ $ db_password = "" ;
6
+
7
+ // Create Connection with exception handling
8
+ try {
9
+ $ conn = new PDO ($ dsn , $ db_user , $ db_password );
10
+ $ conn ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
11
+ echo "Connected <br><hr> " ;
12
+ }
13
+ catch (PDOException $ e ) {
14
+ echo "Connection Failed " . $ e ->getMessage ();
15
+ }
16
+
17
+ if (isset ($ _REQUEST ['update ' ])){
18
+ // checking for empty field
19
+ if (($ _REQUEST ['name ' ] == "" ) || ($ _REQUEST ['roll ' ] == "" ) || ($ _REQUEST ['address ' ] == "" )){
20
+ echo "<small>Fill all fields..</small><hr> " ;
21
+ }
22
+ else {
23
+ // Using Named Placeholder
24
+ $ sql = "UPDATE student SET name = :name, roll = :roll, address = :address WHERE id= :id " ;
25
+
26
+ // Prepared Statement
27
+ $ result = $ conn ->prepare ($ sql );
28
+
29
+ // Bind Parameter to Prepared Statement
30
+ $ result ->bindParam (':name ' , $ name , PDO ::PARAM_STR );
31
+ $ result ->bindParam (':roll ' , $ roll , PDO ::PARAM_INT );
32
+ $ result ->bindParam (':address ' , $ address , PDO ::PARAM_STR );
33
+ $ result ->bindParam (':id ' , $ id , PDO ::PARAM_INT );
34
+
35
+ // Variables and values
36
+ $ name = $ _REQUEST ['name ' ];
37
+ $ roll = $ _REQUEST ['roll ' ];
38
+ $ address = $ _REQUEST ['address ' ];
39
+ $ id = $ _REQUEST ['id ' ];
40
+
41
+ // Execute Prepared Statement
42
+ $ result ->execute ();
43
+
44
+ echo $ result ->rowCount () . " Row Updated <br> " ;
45
+
46
+ // Close Prepared Statement
47
+ unset($ result );
48
+ }
49
+ }
50
+
51
+ ?>
52
+
53
+
54
+ <!DOCTYPE html>
55
+ <html lang="en">
56
+
57
+ <head>
58
+ <meta charset="UTF-8">
59
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
60
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
61
+ <!-- Bootstrap CSS -->
62
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
63
+ crossorigin="anonymous">
64
+ <title>SIPL</title>
65
+ </head>
66
+
67
+ <body>
68
+ <div class="container">
69
+ <div class="row">
70
+ <div class="col-sm-4">
71
+ <?php
72
+ if (isset ($ _REQUEST ['edit ' ])){
73
+ // SELECT with WHERE
74
+ $ sql = "SELECT * FROM student WHERE id = :id " ;
75
+
76
+ // Prepare Statement
77
+ $ result = $ conn ->prepare ($ sql );
78
+
79
+ // Bind parameter
80
+ $ result ->bindParam (':id ' , $ id );
81
+
82
+ $ id = $ _REQUEST ['id ' ];
83
+
84
+ // Execute statement
85
+ $ result ->execute ();
86
+
87
+ // Fetch Single row data
88
+ $ row = $ result ->fetch (PDO ::FETCH_ASSOC );
89
+
90
+ // Close Prepared Statement
91
+ unset($ result );
92
+
93
+ }
94
+ ?>
95
+ <form action="" method="POST">
96
+ <div class="form-group">
97
+ <label for="name">Name</label>
98
+ <input type="text" class="form-control" name="name" id="name" value="<?php if (isset ($ row ['name ' ])){ echo $ row ['name ' ];} ?> ">
99
+ </div>
100
+ <div class="form-group">
101
+ <label for="roll">Roll</label>
102
+ <input type="text" class="form-control" name="roll" id="roll" value="<?php if (isset ($ row ['roll ' ])){echo $ row ['roll ' ];} ?> ">
103
+ </div>
104
+ <div class="form-group">
105
+ <label for="address">Address</label>
106
+ <input type="text" class="form-control" name="address" id="address" value="<?php if (isset ($ row ['address ' ])){ echo $ row ['address ' ];} ?> ">
107
+ </div>
108
+ <input type="hidden" name="id" value="<?php echo $ row ['id ' ] ?> ">
109
+ <button type="submit" class="btn btn-success" name="update">Update</button>
110
+ </form>
111
+
112
+ </div>
113
+ <div class="col-sm-6 offset-sm-2">
114
+ <?php
115
+ // SELECT All Data
116
+ $ sql = "SELECT * FROM student " ;
117
+
118
+ // Prepared Statement
119
+ $ result = $ conn ->prepare ($ sql );
120
+
121
+ // Execute Prepared statement
122
+ $ result ->execute ();
123
+
124
+ if ($ result ->rowCount () > 0 ){
125
+ echo '<table class="table"> ' ;
126
+ echo "<thead> " ;
127
+ echo "<tr> " ;
128
+ echo "<th>ID</th> " ;
129
+ echo "<th>Name</th> " ;
130
+ echo "<th>Roll</th> " ;
131
+ echo "<th>Address</th> " ;
132
+ echo "<th>Action</th> " ;
133
+ echo "</tr> " ;
134
+ echo "</thead> " ;
135
+ echo "<tbody> " ;
136
+ // Fetch all table data
137
+ while ($ row = $ result ->fetch (PDO ::FETCH_ASSOC )){
138
+ echo "<tr> " ;
139
+ echo "<td> " . $ row ['id ' ] . "</td> " ;
140
+ echo "<td> " . $ row ['name ' ] . "</td> " ;
141
+ echo "<td> " . $ row ['roll ' ] . "</td> " ;
142
+ echo "<td> " . $ row ['address ' ] . "</td> " ;
143
+ echo '<td><form action="" method="POST"><input type="hidden" name="id" value= ' . $ row ["id " ] . '><input type="submit" class="btn btn-sm btn-warning" name="edit" value="Edit"></form></td> ' ;
144
+ echo "</tr> " ;
145
+ }
146
+ echo "</tbody> " ;
147
+ echo "</table> " ;
148
+ } else {
149
+ echo "0 Results " ;
150
+ }
151
+ ?>
152
+ </div>
153
+ </div>
154
+ </div>
155
+
156
+ <!-- Optional JavaScript -->
157
+ <!-- jQuery first, then Popper.js, then Bootstrap JS -->
158
+ <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
159
+ crossorigin="anonymous"></script>
160
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
161
+ crossorigin="anonymous"></script>
162
+ <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
163
+ crossorigin="anonymous"></script>
164
+ </body>
165
+ <?php
166
+
167
+ // Close Prepared Statement
168
+ unset($ result );
169
+
170
+ // Close Connection
171
+ $ conn = null
172
+ ?>
173
+
174
+ </html>
0 commit comments