|
| 1 | +<?php |
| 2 | + //creating connection |
| 3 | + $conn = mysqli_connect('localhost','root','','stdinfo'); |
| 4 | + // //checking connection |
| 5 | + // if($conn){ |
| 6 | + // echo "Connection Established"; |
| 7 | + // } |
| 8 | + |
| 9 | + //if click on button take filed value & insert to db |
| 10 | + if(isset($_POST['btn'])){ |
| 11 | + //finding input filed value into variable |
| 12 | + $stdname = $_POST['stdname']; |
| 13 | + $stdreg = $_POST['stdreg']; |
| 14 | + |
| 15 | + //if stdname & stdreg field not empty perform insert operation |
| 16 | + if(!empty($stdname) && !empty($stdreg)){ |
| 17 | + //sql query // stdname string that's why keeping like string/text |
| 18 | + $query = "INSERT INTO student(stdname,stdreg) VALUE('$stdname',$stdreg)"; |
| 19 | + |
| 20 | + //sending data to database |
| 21 | + $createQuery = mysqli_query($conn, $query); |
| 22 | + if($createQuery){ |
| 23 | + echo "Data successfully inserted."; |
| 24 | + } |
| 25 | + } |
| 26 | + else{ |
| 27 | + echo "Field Should not be empty"; |
| 28 | + } |
| 29 | + } |
| 30 | +?> |
| 31 | + |
| 32 | +<!-- code for delete --> |
| 33 | +<?php |
| 34 | + //if click on delete |
| 35 | + if(isset($_GET['delete'])){ |
| 36 | + $stdid = $_GET['delete']; //keeping the delete id in stdid |
| 37 | + $query = "DELETE FROM student WHERE id={$stdid}"; |
| 38 | + $deleteQuery = mysqli_query($conn, $query); |
| 39 | + if($deleteQuery){ |
| 40 | + echo "Data successfully deleted"; |
| 41 | + } |
| 42 | + } |
| 43 | +?> |
| 44 | + |
1 | 45 | <!doctype html>
|
2 | 46 | <html lang="en">
|
3 | 47 | <head>
|
|
11 | 55 | <title>PHP CRUD!</title>
|
12 | 56 | </head>
|
13 | 57 | <body>
|
14 |
| - <div class="container shadow m-5 p-3"> |
15 |
| - <form action="" method="post" class="d-flex justify-content-around"> |
| 58 | + <div class="container shadow m-5 p-4 mx-auto rounded"> |
| 59 | + <form method="post" class="d-flex justify-content-around"> |
16 | 60 | <input class="form-control me-3" type="text" name="stdname" placeholder="Enter Name">
|
17 | 61 | <input class="form-control me-3" type="number" name="stdreg" placeholder="Enter Reg Number">
|
18 | 62 | <input class="btn btn-success" type="submit" value="Submit" name="btn">
|
19 | 63 | </form>
|
20 | 64 | </div>
|
21 | 65 |
|
| 66 | + <div class="container m-5 p-3 mx-auto"> |
| 67 | + <form method="post" class="d-flex justify-content-around"> |
| 68 | + <?php |
| 69 | + if(isset($_GET['update'])){ //if click on update button |
| 70 | + $stdid = $_GET['update']; //geting update id from search query |
| 71 | + $query = "SELECT * FROM student WHERE id={$stdid}"; |
| 72 | + $getData = mysqli_query($conn, $query); //getting data based on query |
| 73 | + |
| 74 | + while($rx=mysqli_fetch_assoc($getData)){ //keep data rx variable afte fetch |
| 75 | + $stdid = $rx['id']; |
| 76 | + $stdname = $rx['stdname']; |
| 77 | + $stdreg = $rx['stdreg']; |
| 78 | + |
| 79 | + ?> |
| 80 | + <input class="form-control me-3" type="text" name="stdname" value="<?php echo $stdname ?>" > |
| 81 | + <input class="form-control me-3" type="number" name="stdreg" value="<?php echo $stdreg ?>"> |
| 82 | + <input class="btn btn-primary" type="submit" value="Update" name="update-btn"> |
| 83 | + <?php |
| 84 | + } //closing previous php while/if backet |
| 85 | + } ?> |
| 86 | + |
| 87 | + <?php |
| 88 | + if(isset($_POST['update-btn'])){ |
| 89 | + $stdname = $_POST['stdname']; |
| 90 | + $stdreg = $_POST['stdreg']; |
| 91 | + |
| 92 | + if(!empty($stdname) && !empty($stdreg)){ |
| 93 | + $query = "UPDATE student SET stdname='$stdname', stdreg=$stdreg WHERE id=$stdid"; |
| 94 | + $updateQuery = mysqli_query($conn, $query); |
| 95 | + // if($updateQuery){ |
| 96 | + // echo "Data Updated successful"; |
| 97 | + // } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + ?> |
| 102 | + </form> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div class="container"> |
| 106 | + <table class="table table-bordered"> |
| 107 | + <tr> |
| 108 | + <th>STD ID</th> |
| 109 | + <th>STD NAME</th> |
| 110 | + <th>Reg No</th> |
| 111 | + <th></th> |
| 112 | + <th></th> |
| 113 | + </tr> |
| 114 | + |
| 115 | + <?php |
| 116 | + //select all query |
| 117 | + $query = "SELECT * FROM student"; |
| 118 | + //reading data from databse |
| 119 | + $readQuery = mysqli_query($conn, $query); |
| 120 | + // if table has more than 0 row then it will read data |
| 121 | + if($readQuery->num_rows >0){ |
| 122 | + // if tables row > 0 read data from db and store the data into rd variable |
| 123 | + while($rd=mysqli_fetch_assoc($readQuery)){ |
| 124 | + //'id' is the table column name which col will be read |
| 125 | + $stdid = $rd['id']; // keeping data from db table to variable |
| 126 | + $stdname = $rd['stdname']; |
| 127 | + $stdreg = $rd['stdreg']; |
| 128 | + |
| 129 | + ?> |
| 130 | + <tr> |
| 131 | + <td><?php echo"$stdid" ?></td> |
| 132 | + <td><?php echo"$stdname" ?></td> |
| 133 | + <td><?php echo"$stdreg" ?></td> |
| 134 | + <td><a href="index.php?update=<?php echo"$stdid" ?>" class="btn btn-info">Update</a></td> |
| 135 | + <!-- passing query parameter id for perform delete while click on delete btn --> |
| 136 | + <td><a href="index.php?delete=<?php echo"$stdid" ?>" class="btn btn-danger">Delete</a></td> |
| 137 | + </tr> |
| 138 | + <?php |
| 139 | + } |
| 140 | + } |
| 141 | + else{ |
| 142 | + echo "No data to show"; |
| 143 | + } |
| 144 | + |
| 145 | + ?> |
| 146 | + <!-- closing whitle & if php backet after using html --> |
| 147 | + </table> |
| 148 | + </div> |
| 149 | + |
22 | 150 | <!-- Optional JavaScript; choose one of the two! -->
|
23 | 151 |
|
24 | 152 | <!-- Option 1: Bootstrap Bundle with Popper -->
|
|
0 commit comments