
Create edit.php for updating a data with method POST AND Action = “edit-process.php”
First retrieve a database crud and table name cruddetails and fetch rows – id,name,address,gender,edu,year
retrieve.php
<?php $conn= mysqli_connect('localhost','root','','crud'); if (!$conn){ die('could not connect mysql:' .msql_error()); } $result = mysqli_query($conn,"SELECT * FROM cruddetails"); ?> <!DOCTYPE html> <html> <head> <title> Retrive data</title> </head> <body> <h4><a href="insert.html">Insert Data</a></h4> <?php if (mysqli_num_rows($result) > 0) { ?> <table> <tr> <td>Name</td> <td>Address</td> <td>Gender</td> <td>Education</td> <td>Year</td> <td>Action</td> </tr> <?php $i=0; while($row = mysqli_fetch_array($result)) { ?> <tr> <td><?php echo $row["name"]; ?></td> <td><?php echo $row["address"]; ?></td> <td><?php echo $row["gender"]; ?></td> <td><?php echo $row["edu"]; ?></td> <td><?php echo $row["year"]; ?></td> <td><a href="edit.php?id=<?php echo $row["id"]; ?>">Update</a></td> </tr> <?php $i++; } ?> </table> <?php } else{ echo "No result found"; } ?> </body> </html>
After creating retrieve.php then create edit file for fetching and updating records having two files
edit.php
<?php $conn= mysqli_connect('localhost','root','','crud'); if (!$conn){ die('could not connect mysql:' .msql_error()); } $id = $_GET ['id']; $query = "SELECT * FROM cruddetails WHERE id ='".$id."'"; $result = mysqli_query($conn,$query) or die (mysqli_error()); $row = mysqli_fetch_assoc($result); $data = explode(',',$row['edu']); $datagender = explode(',',$row['gender']); // print_r($datagender);die; ?> <h1><a href="records.php">All Records</a></h1> <form action="edit-process.php" method="POST"> <input type="hidden" name="id" value="<?php echo $row['id']?>"><br> NAME: <input type="text" name="name" value="<?php echo $row['name']?>"><br> Address: <textarea name="address" cols="20" rows="5"><?php echo $row['address']?></textarea><br> Gender: <input type="radio" name="gender" value="Male" <?php if (in_array("Male", $datagender)) { ?> checked = checked; <?php } ?>>Male <input type="radio" name="gender" value="Male" <?php if (in_array("Female", $datagender)) { ?> checked = checked; <?php } ?>>Female<br> Education: <input type="checkbox" name="edu[]" value="BE" <?php if (in_array("BE", $data)) { ?> checked = checked; <?php } ?> >BE <input type="checkbox" name="edu[]" value="DE" <?php if (in_array("DE", $data)) { ?> checked = checked; <?php } ?> >DE <input type="checkbox" name="edu[]" value="MCA" <?php if (in_array("MCA", $data)) { ?> checked = checked; <?php } ?> >MCA Year: <select name="year"> <option <?php if($row['year']=='2018'){ echo "selected";}?>>2018</option> <option <?php if($row['year']=='2019'){ echo "selected";}?>>2019</option> <option <?php if($row['year']=='2020'){ echo "selected";}?>>2020</option> </select><br> <input type="submit" name="save" value="save"> </form>
edit-process.php
<?php $conn= mysqli_connect('localhost','root','','crud'); if (!$conn){ die('could not connect mysql:' .msql_error()); } if (isset($_POST['save'])) { $id = $_POST['id']; $name = $_POST['name']; $address = $_POST['address']; $gender = $_POST['gender']; $edu = implode(",",$_POST['edu']); $year = $_POST['year']; $sql = "UPDATE cruddetails SET name='$name',address='$address',gender='$gender',edu='$edu',year='$year' WHERE id=".$id; // var_dump($sql); // die; $result = $conn->query($sql); echo "<script>alert('UPDATE successfully');</script>"; echo "<script>window.location.href='records.php'</script>"; } ?>