phpcrud

CRUD Operation in PHP With Live Example!


What is CRUD?

CRUD operations are basic information manipulation for databases. We’ve already learned a way to perform produce (i.e. insert), read (i.e. select), update, and delete operations in previous chapters. during this tutorial, we’ll produce a straightforward PHP application to perform these operations on a MySQL information table in one place.

What’s CRUD Full Form?

CRUD is a word form for Create, Read, Update, and Delete.

CRUD operation in PHP MySQL

Well, let’s begin by making the table that we’ll use all told of our examples.

  1. Create new Database
  2. Create new table
  3. Create index.php file
  4. Create server.php file
  5. Create style.css file

1.Create new Database

create new database ( crud ). crud is database name.


2.Create new table

create new table ( info ). info is table name.


3.Create index.php file

create an index.php file on your computer and create a form now add PHP in your index.php file and include your server.php file also.

<?php 
include('server.php');

// fetch the record to be updated
	if (isset($_GET['edit'])) {
		$id = $_GET['edit'];
		$edit_state = true;
		$rec = mysqli_query($db, "SELECT * FROM info WHERE  id='$id'" );
		$record =mysqli_fetch_array($rec);
		$name = $record['name'];
		$address = $record['address'];
		$id = $record['id'];
	}

?>

<!DOCTYPE html>
<html>
<head>
	<title>crud one page</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<style type="text/css">
		
.msg{
	margin:30px auto;
	padding: 10px;
	border-radius: 5px;
	color: yellow;
	background:blue;
	width: 50%;
	text-align: center;

	</style>
</head>
<body>
<?php if (isset($_SESSION['msg'])): ?>
<div class="msg">
	<?php 
		echo $_SESSION['msg'];
		unset($_SESSION['msg']);
	?>
</div>
<?php endif ?>

<table>
	<thead>
	<tr>
		</tr>
		<th>name</th>
		<th>address</th>
		<th colspan="2">action</th>
	</tr>
	</thead>
	<tbody>
<?php while ($row = mysqli_fetch_array($results)) {  ?>
 		<tr>
		<td><?php echo $row['name']; ?></td>
		<td><?php echo $row['address']; ?></td>
		<td>
			<a href="index.php?edit=<?php echo $row['id']; ?>">edit</a>
		</td>
		<td>
			<a href="server.php?del=<?php echo $row['id']; ?>">delete</a>
		</td>
	</tr>
 <?php	
}
?>

	
	</tbody>
</table>

<form method="post" action="">
	<input type="hidden" name="id" value="<?php echo $id; ?>">
	<div class="input-group">
		<label>Name</label>
		<input type="text" name="name" value="<?php echo $name; ?>" >
	</div>
	<div class="input-group">
		<label>Address</label>
		<input type="text" name="address" value="<?php echo $address; ?>">
	</div>
	<div class="input-group">
		<?php if ($edit_state == false) : ?>
		<button type="submit" name="save" class="btn">save</button>
		<?php else: ?>
		<button type="submit" name="update" class="btn">update</button>
		<?php endif ?>
	</div>
</form>


</body>
</html>

output:-


4.Create server.php file

now create server.php file in computer and PHP code written first insert query, second view query, third update query, and last four delete query insert in server.php

<?php 
	session_start();

// initialize variables
	$name = "";
	$address = "";
	$id = 0;
	$edit_state = false;

// connect to database

// if save btn is click
$db = mysqli_connect('localhost','root','','crud');


if (isset($_POST['save'])) {
	$name = $_POST['name'];
	$address = $_POST['address'];

	$query = "INSERT INTO info (name,address) VALUES ('$name','$address')";
	mysqli_query($db,$query);
	$_SESSION['msg']  = "address delete";
			header('location:index.php');//redirect to index page after inserting 
}

//update record
		if (isset($_POST['update'])) {
			$name = mysqli_real_escape_string($db, $_POST['name']);
			$address = mysqli_real_escape_string($db, $_POST['address']);
			$id = mysqli_real_escape_string($db, $_POST['id']);

			$data= mysqli_query($db, "UPDATE info SET name='$name', address='$address' WHERE id='$id' ");
			if ($data) {
					echo "<script>alert('update')</script>";
				}
		}

// delete record

	if (isset($_GET['del'])) {
		$id = $_GET['del'];
		mysqli_query($db, "DELETE FROM info WHERE id='$id'");
				$_SESSION['msg']  = "address delete";
			header('location:index.php');

	}

//retrieve records
	$results = mysqli_query($db, "SELECT * FROM info");

?>

5.Create style.css file

You add steel.css to make your crud table look good.

body{
	font-size: 20px;
}
table{
	width: 50%;
	margin:30px auto;
	border-collapse: collapse;
	text-align: left;
}
tr{
	border-bottom: 1px solid black;
}
tr, td{
	border: none;
	height: 30px;
	padding: 3px;
}
tr:hover {
	background:#f5f5f5;
}
form{
	width: 50%;
	margin: 50px auto;
	text-align: left;
	padding: 20px;
	border: 1px solid black;
	border-radius: 5px;
}
.input-group{
	margin: 10px 0px 10px 0px;
}
.input-group label{
	display: block;
	text-align: left;
	margin: 3px;
}
.input-group input{
	height: 30px;
	width: 93%;
	padding: 5px 10px;
	font-size: 16px;
	border-radius: 5px;
	border: 1px solid gray;
}
.btn{
padding: 10px;
font-size: 15px;
color: white;
background:#5F9EA0;
border:none;
border-radius: 5px; 
}

}

How to Create CRUD ??

CRUD means is Create, Read, Update, and Delete.

CRUD ( Insert, Show, Update and delete ) in PHP zip.file here Download link.



Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *