{tocify} $title={Table of Contents}
Introduction:
In this post we have learned how to view, update and delete data.
If you do not know how to add data to the database, you can see my previous post-How to insert data into database using php and html? click this link.
Note :- The database is the same as in the previous post. If you haven’t seen that post, you can see it by clicking on the link above
View Data:
Step 1:-Open the code editor you choose and create a new file view.php
Write this code on your code editor for view:
<?php
$conn = mysqli_connect("localhost", "root", "", "college");
$q = "select*from student";
$rusult = mysqli_query($conn, $q);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PoorWebDev</title>
<link rel="stylesheet" href="style.css">
</head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.form-container {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: space-around;
}
.form-input {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 5px;
}
input[type="submit"] {
width: 100%;
margin-top: 10px;
border: none;
background: #6ea1ff;
color: #ffff;
padding: 8px 0;
height: auto;
}
input {
height: 25px;
border-radius: 4px;
outline: none;
border: 1px solid gray;
}
table,
tr,
td,
th {
border: 1px solid gray;
border-collapse: collapse;
padding: 2px 5px;
}
</style>
<body>
<div class="form-container">
<form action="insert.php" method="post">
<div class="form-input">
<label for="">Student Name</label>
<input type="text" name="name" id="">
<label for="">Student Surmane</label >
<input type="text" name="surmane" id="">
<label for="">Student Age</label >
<input type="number" name="age" id="">
<label for="">City</label >
<input type="text" name="city" id="">
<label for="">Email</label >
<input type="email" name="email" id="">
<label for="">Mobile Number</label >
<input type="text" name="mobileno" id="">
</div>
<input type="submit" name="insert" value="Submit">
</form>
<div class="table-content">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
<th>City</th>
<th>Email</th>
<th>Mobile no</th>
<th>Action</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($rusult)) {
?>
<tr>
<td>
<?php echo $row['ID']; ?>
</td>
<td>
<?php echo $row['Name']; ?>
</td>
<td>
<?php echo $row['Surname']; ?>
</td>
<td>
<?php echo $row['Age']; ?>
</td>
<td>
<?php echo $row['City']; ?>
</td>
<td>
<?php echo $row['Email']; ?>
</td>
<td>
<?php echo $row['Mobile_no']; ?>
</td>
<td>
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $row['ID'];?>">
<button name="delete">Delete</button>
<button name="update">update</button>
</form>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
Step 2:- Create new file update.php
Write this code for update and delete
<?php
$conn = mysqli_connect("localhost", "root", "", "college");
$q = "select*from student";
$rusult = mysqli_query($conn, $q);
if(isset($_POST["update"]) && isset($_POST["id"]) ){
$id = $_POST["id"];
$q="update student set name='$name',Surname='$surnane',Age='$age',City='$city',Email='$email',Mobile_no='$mobile'WHERE id='$id'";
mysqli_query($conn,$q);
header("location:Student.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.form-container {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: space-around;
}
.update-form h1 {
text-align: center;
padding-bottom: 10px;
}
.form-input {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 5px;
}
input[type="submit"] {
width: 100%;
margin-top: 10px;
border: none;
background: #6ea1ff;
color: #ffff;
padding: 8px 0;
height: auto;
}
input {
height: 25px;
border-radius: 4px;
outline: none;
border: 1px solid gray;
}
</style>
</head>
<body>
<div class="form-container">
<div class="update-form" id="update-form">
<h1>update</h1>
<?php
$id=$_POST["id"];
$q="select * from student where ID =$id";
$result=mysqli_query($conn,$q);
$row=mysqli_fetch_assoc($result);
if(isset($_POST['delete'])){
$q2="delete from student where ID ='$id'";
mysqli_query($conn,$q2);
header("location:Student.php") ;
}
?>
<form action="" method="post">
<div class="form-input">
<input type="hidden" name="id" value="<?php echo $row['ID'] ?>">
<label for="">Student Name</label>
<input type="text" name="name" value="<?php echo $row['Name'] ?>">
<label for="">Student Surname</label>
<input type="text" name="surname" value="<?php echo $row['Surname'] ?>">
<label for="">Student Age</label>
<input type="number" name="age" value="<?php echo $row['Age'] ?>">
<label for="">City</label>
<input type="text" name="city" value="<?php echo $row['City'] ?>">
<label for="">Email</label>
<input type="email" name="email" value="<?php echo $row['Email'] ?>">
<label for="">Mobile Number</label>
<input type="text" name="mobileno" value="<?php echo $row['Mobile_no'] ?>">
</div>
<input type="submit" name="update">
</form>
</div>
</div>
</body>
</html>