-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
161 lines (141 loc) · 6.05 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
<?php
//creating connection
$conn = mysqli_connect('localhost','root','','stdinfo');
// //checking connection
// if($conn){
// echo "Connection Established";
// }
//if click on button take filed value & insert to db
if(isset($_POST['btn'])){
//finding input filed value into variable
$stdname = $_POST['stdname'];
$stdreg = $_POST['stdreg'];
//if stdname & stdreg field not empty perform insert operation
if(!empty($stdname) && !empty($stdreg)){
//sql query // stdname string that's why keeping like string/text
$query = "INSERT INTO student(stdname,stdreg) VALUE('$stdname',$stdreg)";
//sending data to database
$createQuery = mysqli_query($conn, $query);
if($createQuery){
echo "Data successfully inserted.";
}
}
else{
echo "Field Should not be empty";
}
}
?>
<!-- code for delete -->
<?php
//if click on delete
if(isset($_GET['delete'])){
$stdid = $_GET['delete']; //keeping the delete id in stdid
$query = "DELETE FROM student WHERE id={$stdid}";
$deleteQuery = mysqli_query($conn, $query);
if($deleteQuery){
echo "Data successfully deleted";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>PHP CRUD!</title>
</head>
<body>
<div class="container shadow m-5 p-4 mx-auto rounded">
<form method="post" class="d-flex justify-content-around">
<input class="form-control me-3" type="text" name="stdname" placeholder="Enter Name">
<input class="form-control me-3" type="number" name="stdreg" placeholder="Enter Reg Number">
<input class="btn btn-success" type="submit" value="Submit" name="btn">
</form>
</div>
<div class="container m-5 p-3 mx-auto">
<form method="post" class="d-flex justify-content-around">
<?php
if(isset($_GET['update'])){ //if click on update button
$stdid = $_GET['update']; //geting update id from search query
$query = "SELECT * FROM student WHERE id={$stdid}";
$getData = mysqli_query($conn, $query); //getting data based on query
while($rx=mysqli_fetch_assoc($getData)){ //keep data rx variable afte fetch
$stdid = $rx['id'];
$stdname = $rx['stdname'];
$stdreg = $rx['stdreg'];
?>
<input class="form-control me-3" type="text" name="stdname" value="<?php echo $stdname ?>" >
<input class="form-control me-3" type="number" name="stdreg" value="<?php echo $stdreg ?>">
<input class="btn btn-primary" type="submit" value="Update" name="update-btn">
<?php
} //closing previous php while/if backet
} ?>
<?php
if(isset($_POST['update-btn'])){
$stdname = $_POST['stdname'];
$stdreg = $_POST['stdreg'];
if(!empty($stdname) && !empty($stdreg)){
$query = "UPDATE student SET stdname='$stdname', stdreg=$stdreg WHERE id=$stdid";
$updateQuery = mysqli_query($conn, $query);
// if($updateQuery){
// echo "Data Updated successful";
// }
}
}
?>
</form>
</div>
<div class="container">
<table class="table table-bordered">
<tr>
<th>STD ID</th>
<th>STD NAME</th>
<th>Reg No</th>
<th></th>
<th></th>
</tr>
<?php
//select all query
$query = "SELECT * FROM student";
//reading data from databse
$readQuery = mysqli_query($conn, $query);
// if table has more than 0 row then it will read data
if($readQuery->num_rows >0){
// if tables row > 0 read data from db and store the data into rd variable
while($rd=mysqli_fetch_assoc($readQuery)){
//'id' is the table column name which col will be read
$stdid = $rd['id']; // keeping data from db table to variable
$stdname = $rd['stdname'];
$stdreg = $rd['stdreg'];
?>
<tr>
<td><?php echo"$stdid" ?></td>
<td><?php echo"$stdname" ?></td>
<td><?php echo"$stdreg" ?></td>
<td><a href="index.php?update=<?php echo"$stdid" ?>" class="btn btn-info">Update</a></td>
<!-- passing query parameter id for perform delete while click on delete btn -->
<td><a href="index.php?delete=<?php echo"$stdid" ?>" class="btn btn-danger">Delete</a></td>
</tr>
<?php
}
}
else{
echo "No data to show";
}
?>
<!-- closing whitle & if php backet after using html -->
</table>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
-->
</body>
</html>