Crud Operations in PHP Using MySQL Example
Mar 11, 2022 . Admin
Hello Friends,
I am going to explain you How To create simple crud operation in PHP. You will learn How to Create simple crud operations in PHP using MySQL. In side this article we will see Creating simple crud operation in PHP source code.
This article will give you simple example crud operations in PHP using Xampp. We will use get How To Make Simple CRUD Using PHP and MySQL Easily.
You can use from How to Create PHP Simple CRUD Application Script - Tutorials . I will give you simple example crud operation in PHP.
So, let's see bellow solution:
conection.php<?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("connection failed:" .mysqli_connect_error()); echo "connection successfull"; } ?>index.php
<?php session_start(); include "conection.php"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Crud Operations in PHP Using MySQL Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style type="text/css"> table { width: 100%; text-align: center; } tr td { border-bottom: 1px solid black; height: 50px; } </style> </head> <body> <?php if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"])) { $msg=$_SESSION["msg"]; echo "<div class='msgbox alert alert-success p-1 text-center m-0' style='background-color:#9bffbbfa; width: 300px; height: 30px'>".$msg."</div>"; unset($_SESSION['msg']); session_destroy(); } ?> <div class="container mt-3"> <div class="card"> <div class="card-header"> <div class="container mt-2"> <div class="row"> <div class="col-md-11"> <h1>Crud Operations in PHP Using MySQL Example - Mywebtuts.com</h1> </div> <div class="col-md-1 text-end"> <a href="table-insert.php" type="button" class="btn btn-success"><i class="fa fa-plus-square-o " aria-hidden="true"></i></a> </div> </div> </div> </div> <div class="card-body"> <table class='table table-striped'> <tr> <th>ID</th> <th> F-Name </th> <th> L-name </th> <th>email</th> <th>Gender</th> <th>Action</th> </tr> <?php $sql = "SELECT * FROM student"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()){ echo"<tr>"; echo "<td>" .$row['id']."</td>"; echo "<td>" .$row['firstname']."</td>"; echo "<td>".$row['lastname']."</td>"; echo "<td>".$row['email']."</td>"; echo "<td>".$row['gender']."</td>"; echo "<td class='icon-td'>"; echo '<a href="update.php?id='. $row['id'] .'"title="Update Record" class="btn btn-success" data-toggle="tooltip"><span class="fa fa-pencil"></span></a>'; echo '<a href="delete.php?id='. $row['id'] .'" title="Delete Record" class="btn btn-danger mx-1" data-toggle="tooltip"><span class="fa fa-trash"></span></a>'; echo "</td>"; echo "</tr>"; } }else{ echo " <tr> <td colspan='6'>No Data Avilable in Table!</td> </tr>"; } mysqli_close($conn); ?> </table> </div> </div> </div> </body> </html>table-insert.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>insert</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <style type="text/css"> .error { color: red; } </style> </head> <body> <?php $fnameErr = $lnameErr = $genderErr = $emailErr = ""; $fname = $lname = $gender = $email= ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST['fname'])) { $fnameErr = "first name is required!"; } if (empty($_POST['lname'])) { $lnameErr = "last name is required!"; } if (empty($_POST['email'])) { $emailErr = "email is required!"; } if (empty($_POST['gender'])) { $genderErr = "gender is required!"; } if (empty($fnameErr)&&empty($lnameErr)&&empty($genderErr)) { session_start(); include "conection.php"; $firstname = $_POST['fname']; $lastname = $_POST['lname']; $email = $_POST['email']; $gender = $_POST['gender']; $sql = "INSERT INTO student (firstname,lastname,gender,email) VALUES('$firstname','$lastname','$gender','$email')"; if ($conn->query($sql)===TRUE) { $_SESSION['msg'] = 'Data Insert Successfully'; header('location:index.php'); }else{ echo "Error: " .$sql . "<br>" . mysqli_error($conn); } $conn->close(); } } ?> <div class="container mt-5 d-flex justify-content-center"> <div class="card" style="width:500px; height: 500px;"> <div class="card-header"> <h1>Enter data</h1> </div> <div class="card-body"> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <div class="my-3"> <label for="fname">first name</label> <span class="error">*<?php echo $fnameErr ?></span> <input type="text" name="fname" id="fname" class="form-control" placeholder="Enter Fname"> </div> <div class="my-3"> <label for="lname">last name</label> <span class="error">*<?php echo $lnameErr ?></span> <input type="text" name="lname" id="lname" class="form-control" placeholder="Enter lname"> </div> <div class="my-3"> <label for="email">email</label> <span class="error">*<?php echo $emailErr ?></span> <input type="text" name="email" id="email" class="form-control" placeholder="Enter email"> </div> <div class="my-3"> <label for="gender">gender : </label> <span class="error">*<?php echo $genderErr ?></span><br> <input type="radio" name="gender" id="gender" value="male">male <input type="radio" name="gender" id="gender" value="female">female </div> </div> <div class="card-footer text-end"> <input type="submit" name="save" class="btn btn-primary"> <a href="index.php" class="btn btn-danger">cancle</a> </div> </div> </div> </body> </html>update.php
<?php include "conection.php"; $id = $_GET['id']; $sql = "SELECT * FROM student WHERE id = $id"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); }else{ echo "0 result"; } $conn->close(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Update Record</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <div class="container d-flex justify-content-center mt-5"> <div class="card " style="width:500px"> <div class="card-header"> <h2>Update Record</h2> <p>Please edit the input values and submit to update the record.</p> </div> <div class="card-body"> <form action="update-crude.php" method="post"> <div class="mt-2"> <label>firstname</label> <input type="text" name="Fname" class="form-control" value="<?php echo $row['firstname']; ?>" required=""> </div> <div class="mt-2"> <label>lastname</label> <input type="text" name="Lname" class="form-control" value="<?php echo $row['lastname']; ?>" required=""> </div> <div class="mt-2"> <label>email</label> <input type="text" name="email" class="form-control" value="<?php echo $row['email']; ?>" required=""> </div> <div class="mt-2"> <label>gender</label> <input type="radio" name="G" value="female" <?php echo $row['gender'] == 'female' ? "checked":""; ?> >female <input type="radio" name="G" value="male" <?php echo $row['gender'] == 'male' ? "checked":""; ?> >male </div> <input type="hidden" name="id" value="<?php echo $row['id']; ?>"> </div> <div class="card-footer text-end"> <input type="submit" name="save" class="btn btn-primary" > <a href="index.php" class="btn btn-danger">Cancel</a> </div> </div> </div> </body> </html>update-crude.php
<?php session_start(); include "conection.php"; $firstname = $_POST['Fname']; $lastname = $_POST['Lname']; $email = $_POST['email']; $gender = $_POST['G']; $id = $_POST['id']; $sql = "UPDATE student SET firstname='$firstname', lastname='$lastname', email='$email', gender='$gender' WHERE id=$id"; if ($conn->query($sql)=== TRUE) { $_SESSION["msg"]="record updated successfully"; header("location:index.php"); }else{ echo "Error updating Record".$conn->error; } $conn->close; ?>delete.php
<?php session_start(); include "conection.php"; $id = $_GET['id']; $sql = "DELETE FROM student WHERE id = '$id'"; $result = $conn->query($sql); $conn->close(); $_SESSION['msg'] = 'Data Delete Successfully'; header("location:index.php") ?>Output:
It will help you...