Welcome To ITSolutionsGuides,
Lets see how to create a Simple CRUD Operation In PHP using it solutions guides. Create simple php crud operations for better understanding of the php with create , read , update , delete operations. Lets see how to create a Simple CRUD Operation In PHP using it solutions guides. Using mysql database queries for the CRUD operations. Create simple php crud operations for better understanding of the php with create , read , update , delete operations.
Lets create the connection file by providing all the database informations required for the connection. For Detailed explanation about the database connection refer here
dbconfig.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Lets create the form for inserting the data into the database using the form. While clicking the submit button we should insert the data in the database so that we need to make the connection therefore we should include the connection file first and then use the INSERT query to insert the data in the database.
create-student.php
<!DOCTYPE html>
<html>
<title>Student Database</title>
<body>
<h2>Student Form</h2>
<form action="" method="POST">
<fieldset>
<legend>Student information:</legend>
Name:<br>
<input type="text" name="name"> <br>
Age:<br>
<input type="text" name="age"> <br>
Email:<br>
<input type="email" name="email"><br>
<br><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>
<?php
include "dbconfig.php";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$sql = "INSERT INTO `students`(`name`, `age`, `email`) VALUES ('$name','$age','$email')";
$result = $conn->query($sql);
if ($result == TRUE) {
echo "New record created successfully.";
header('Location: view-student.php');
}else{
echo "Error:". $sql . "<br>". $conn->error;
}
$conn->close();
}
?>
Lets create the file to display all the records in the database table with edit and delete button. Lets use the SELECT query to fetch the table data and display as row in a table using the while .
view-student.php
<?php
include "dbconfig.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>Student Database</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Student Details</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><a class="btn btn-info" href="update-student.php?id=<?php echo $row['id']; ?>">Edit</a>
<a class="btn btn-danger" href="delete-student.php?id=<?php echo $row['id']; ?>">Delete</a>
</td>
</tr>
<?php }
}
?>
</tbody>
</table>
</div>
</body>
</html>
On submitting the form the value should gets updated using the UPDATE query.
update-student.php
<?php
include "dbconfig.php";
if (isset($_POST['update'])) {
$stu_id = $_POST['stu_id'];
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$sql = "UPDATE `students` SET `name`='$name',`age`='$age',`email`='$email' WHERE `id`='$stu_id'";
$result = $conn->query($sql);
if ($result == TRUE) {
echo "Record updated successfully.";
header('Location: view-student.php');
}else{
echo "Error:" . $sql . "<br>" . $conn->error;
}
}
if (isset($_GET['id'])) {
$stu_id = $_GET['id'];
$sql = "SELECT * FROM students WHERE id='$stu_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$name = $row['name'];
$age = $row['age'];
$email = $row['email'];
}
?>
<h2>Student details Update Form</h2>
<form action="" method="post">
<fieldset>
<legend>Personal information:</legend>
Name:<br>
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="hidden" name="stu_id" value="<?php echo $id; ?>">
<br>
Age:<br>
<input type="text" name="age" value="<?php echo $age; ?>">
<br>
Email:<br>
<input type="email" name="email" value="<?php echo $email; ?>">
<br><br>
<input type="submit" value="Update" name="update">
</fieldset>
</form>
</body>
</html>
<?php
} else{
header('Location: view-student.php');
}
}
?>
Lets delete the record by using the DELETE query and also return to view-student.php page once the query is executed successfully and also return error if the query faces any error on execution.
delete-student.php
<?php
include "dbconfig.php";
if (isset($_GET['id'])) {
$stu_id = $_GET['id'];
$sql = "DELETE FROM students WHERE id ='$stu_id'";
$result = $conn->query($sql);
if ($result == TRUE) {
echo "Record deleted successfully.";
header('Location: view-student.php');
}else{
echo "Error:" . $sql . "<br>" . $conn->error;
}
}
?>
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides