CRUD Operation for Beginners in PHP & MySQL

 Hello Friends, As we know CRUD Operation in PHP is most important part for the Beginners. CRUD stands for CREATE, READ, UPDATE & DELETE data/records from the database. All you should know these basic operations we perform in PHP. It tells us full about the MySQL interaction with the frontpage. I am sharing you guys all the codes we have to write to perform the CRUD?

php crud ermansih

First of all we will create index.php file which will looks like as type of registration form. See below image.

php crud ermansih

Step 1: index.php

<?php require 'header.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<center><h2>Registration form</h2>
<div>
<form method="post" action="">
<label for="name">Full Name</label>
<input type="name" id="name" name="name" placeholder="Your name.." required="">

<label for="email">E-Mail</label> <input type="email" id="email" name="email" placeholder="Your E-Mail.." required=""> <label for="phone">Email</label> <input type="phone" id="phone" name="phone" placeholder="Your Phone.." required=""> <input type="submit" value="Submit" name="submit"> </form> </div> </body> </html> <?php require 'conn.php'; if (isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $sql = "INSERT INTO `crud` (`name`, `email`, `phone`) VALUES ('$name', '$email', '$phone')"; mysqli_query($conn,$sql); echo "Registration Successful"; }else { echo "Please enter the details"; }
?>

Step 2: Create Database & Connections

conn.php

<?php
$conn = mysqli_connect("localhost","root","","php_crud");
?>

Now we will create the database as it will looks like


Here i have put my database name crud so guys you can keep anyname as you want.

Step 3: display.php

<?php require 'conn.php';
$sql = "SELECT * FROM `crud`";
$result = mysqli_query($conn,$sql);
?>

<?php require 'header.php'; ?> <html> <head> <meta charset="utf-8"> <title>Registration Form</title> </head> <body> <center><h2>All Details</h2> <table border="2" style="border: 3px red solid; width: 100% height: 100%;"> <tr> <th>Name</th> <th>E-mail</th> <th>Phone</th> <th>Operations</th> </tr> <?php while ($row = mysqli_fetch_assoc($result)){ echo "<tr>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['email']."</td>"; echo "<td>".$row['phone']."</td>"; echo '<td><a href="update.php?action=update&id='.$row['id'].'"><button>Edit</button></a> <a href="delete.php?action=delete&id='.$row['id'].'"><button>Delete</button></a></td>'; } ?> </table><br> </center> </body>
</html>

Output:

Step 4: update.php

<?php require 'conn.php';
$id = $_GET['id'];
$sql = "SELECT * FROM crud WHERE id = {$id}";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
?>
<?php require 'header.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<center><h2>Update Details</h2>
<div>
<form method="post" action="">
<label for="name">Full Name</label>
<input type="name" id="name" name="name" value="<?php echo $row['name']; ?>" required="">

<label for="email">E-Mail</label> <input type="email" id="email" name="email" value="<?php echo $row['email']; ?>" required=""> <label for="phone">Email</label> <input type="phone" id="phone" name="phone" value="<?php echo $row['phone']; ?>" required=""> <input type="submit" value="Submit" name="submit"> </form> </div> </body> </html> <?php if (isset($_POST['submit'])) { $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $update = "UPDATE crud SET name = '{$name}', phone = '{$phone}',email = '{$email}' WHERE id = {$id}"; $res = mysqli_query($conn,$update); echo "<center>Details Updated Successfully.</center>"; }else { echo "Please enter the details"; }
?>

Step 5: delete.php

This file will help you to perform delete operation whatever records you want to delete from the database.

<?php require 'conn.php'; 
$id = $_GET['id'];
$sql = "DELETE FROM crud WHERE id=$id";
$result = mysqli_query($conn,$sql);

echo "Records Successfully Deleted"; ?> <br><br>
<a href="display.php"><button>Back to Records</button></a>
Step 5: header.php

Header section contains menu bar if you want to make it attractive.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>

<ul> <li><a class="active" href="#home">Home</a></li> <li><a href="display.php">View Records</a></li> <li><a href="index.php">Add New Records</a></li> </ul> </body>
</html>

Step 7: style.css

This is css style file so do not forgot it to save as extension .css. It helps you to style the page attractive format.

input[type=email], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=name], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=phone], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}

input[type=submit] { width: 100%; background-color: #3215d4; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover { background-color: #3215d4; } div { border-radius: 5px; background-color: #f2f2f2; padding: 20px; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #111;
}

Download zip file Click here

Post a Comment

0 Comments