डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

Full Stack Web Development with Laravel

Full Stack Web Development with Laravel

Affiliate Program

Affiliate Program

PHP CRUD operations

First of all, create a project structure as given below.
  • crud(Folder)
    • server (Folder)
      • connect.php
    • bootstrap.php
    • delete_user.php
    • edit_user.php
    • footer.php
    • header.php
    • index.php
    • navbar.php
    • register.php
    • users.php
    • validate.php

Visit the given URL to create a new database

localhost/phpmyadmin

Create a new database with the name crud.

Visit the given URL to run this project

localhost/crud

1) connect.php

<?php 
// code to connect front end with back end
$server="127.0.0.1";
$user="root";
$password="";
$database="crud";

// pointer(data manipulate using this pointer)
 $conn = mysqli_connect($server, $user, $password, $database);

 if($conn){
 	// echo "connected";

 	$query ="CREATE TABLE IF NOT EXISTS users(
			id INT(10) AUTO_INCREMENT PRIMARY KEY,
			fname VARCHAR(120) NOT NULL,
			lname VARCHAR(120) NOT NULL,
			email VARCHAR(120) NOT NULL,
			password VARCHAR(120) NOT NULL
			 )";

$result = mysqli_query($conn, $query);

if($result){
	// echo "Users table created successfully";
}
else{
	// echo "something wrong on creation of users table.";
}

 }
 else{
 	echo "not connected";
 }
?>

2) header.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<?php include_once("bootstrap.php"); ?>
</head>
<body>

3) footer.php

</body>
</html>

4) bootstrap.php

<!-- css -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<!-- js -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">

5) navbar.php

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container">
    <a class="navbar-brand" href="index.php">CRUD</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
          </ul>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled">Disabled</a>
        </li>
      </ul>


      <ul class="navbar-nav  mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="register.php">Register</a>
        </li>

        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="users.php">Users</a>
        </li>
        
      </ul>
      
    </div>
  </div>
</nav>

6) index.php

<?php include_once("header.php"); ?>
<?php include_once("navbar.php"); ?>
<div class="container">
	<div class="row">
		<div class="col-sm-12">
			<h1>Home page</h1>
		</div>
	</div>
</div>
<?php include_once("footer.php"); ?>

7) register.php

<?php include_once("header.php"); ?>
<?php include_once("navbar.php"); ?>
<div class="container">
	<div class="row">
		<div class="col-sm-12">
			<h1>Register page</h1>
		</div>
	</div>

	<div class="row">
		<div class="col-sm-4 mx-auto">

			<div class="card">
				<div class="card-body shadow">

<?php 
require_once("server/connect.php");
require_once("validate.php");

	if(isset($_POST['register'])){

		$fname=validate($_POST['fname']);
		$lname=validate($_POST['lname']);
		$email=validate($_POST['email']);
		$password=validate($_POST['password']);

		$query="INSERT INTO users(fname, lname, email, password)VALUES('$fname','$lname','$email','$password')";

		$result = mysqli_query($conn, $query);
		if($result){
			echo "New user registered";
		}
		else{
			echo "Something wrong";
		}


		
	}
 ?>
			<form action="register.php" method="POST">
				<div>
					<label for="fname">First name</label>
					<input type="text" name="fname" class="form-control" required>
				</div>

				<div>
					<label for="lname">Last name</label>
					<input type="text" name="lname" class="form-control" required>
				</div>

				<div>
					<label for="email">Email id</label>
					<input type="text" name="email" class="form-control" required>
				</div>

				<div>
					<label for="password">Password</label>
					<input type="text" name="password" class="form-control" required>
				</div>

				<div class="my-5 text-center">
					<input type="submit" name="register" value="Register" class="btn btn-primary">
				</div>
			</form>
					
				</div>
			</div>

			
		</div>
	</div>
</div>
<?php include_once("footer.php"); ?>

8) users.php

<?php include_once("header.php"); ?>
<?php include_once("navbar.php"); ?>
<div class="container">
	<div class="row">
		<div class="col-sm-12">
			<h1>Users page</h1>
		</div>
	</div>

	<div class="row">
		<div class="col-sm-12">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>First name</th>
						<th>Last name</th>
						<th>Email id</th>
						<th>Password</th>
						<th><i class="bi bi-gear"></i></th>
					</tr>
				</thead>
				<tbody>

					<?php 
					require_once("server/connect.php"); 
					$query = "SELECT * FROM users ORDER BY id DESC";
					$result = mysqli_query($conn, $query);
					if($result){
						while($rows = mysqli_fetch_array($result)){
							$id= $rows["id"];
							$fname = $rows['fname'];
							$lname = $rows['lname'];
							$email = $rows['email'];
							$password = $rows['password'];
					?>

					<tr>
						<td><?=$fname?></td>
						<td><?=$lname?></td>
						<td><?=$email?></td>
						<td><?=$password?></td>
						<td>
							<a href="edit_user.php?id=<?=$id?>">
								<i class="bi bi-pencil-square"></i>
							</a>


							<a href="delete_user.php?id=<?=$id?>">
								<i class="bi bi-trash3-fill"></i>
							</a>
						</td>
						
					</tr>


					<?php 
						}
					}
					?>

					
				</tbody>
			</table>
		</div>
	</div>
</div>
<?php include_once("footer.php"); ?>

9) delete_user.php

<?php 
// get id from url
$id= $_GET['id'];
// import connection
require_once("server/connect.php");
$query="DELETE FROM users WHERE id='$id'";
// run query
mysqli_query($conn, $query);
$url="users.php";
// redirect to users.php page
header("Location: $url");
exit();
 ?>

10) edit_user.php

<?php include_once("header.php"); ?>
<?php include_once("navbar.php"); ?>
	<h1>Edit user</h1>
	<?php 
	// for connection handler
	require_once("server/connect.php");
	$id = $_GET['id'];
	// echo $id;
	 ?>

	 <?php 
	 // for update
require_once("validate.php");

	 if(isset($_POST['update'])){
		$fname=validate($_POST['fname']);
		$lname=validate($_POST['lname']);
		$email=validate($_POST['email']);
		$password=validate($_POST['password']);

		$query="UPDATE users SET fname='$fname', lname='$lname', email='$email', password='$password' WHERE id='$id'";
		$result = mysqli_query($conn, $query);
		if($result){
			$url="users.php";
			header("Location:$url");
			exit();
		}
		else{
			echo "Something wrong";
		}	 	
	 }

	  ?>


	 <div class="container">
	 	<div class="row">
	 		<div class="col-sm-4 mx-auto">


<div class="card">
	<div class="card-body">

<form action="edit_user.php?id=<?=$id?>" method="POST">

<?php 
// fetch old data
$query="SELECT * FROM users WHERE id='$id'";
// run query
$result=mysqli_query($conn, $query);
// fetch record
$row= mysqli_fetch_array($result);
// print_r($row);
 ?>

				<div>
					<label for="fname">First name</label>
					<input type="text" name="fname" class="form-control" 
					value="<?=$row['fname']?>" required>
				</div>

				<div>
					<label for="lname">Last name</label>
					<input type="text" name="lname" class="form-control"
					value="<?=$row['lname']?>" required>
				</div>

				<div>
					<label for="email">Email id</label>
					<input type="text" name="email" class="form-control"
					value="<?=$row['email']?>" required>
				</div>

				<div>
					<label for="password">Password</label>
					<input type="text" name="password" class="form-control"
					value="<?=$row['password']?>" required>
				</div>
			
				<div class="my-5 text-center">
					<input type="submit" name="update" value="Update" class="btn btn-primary">
				</div>

			</form>		
	</div>
</div>	 			
	 		</div>
	 	</div>
	 </div>
<?php include_once("footer.php"); ?>

11) validate.php

<?php 
// PREVENTION FOR HACKER
function validate($data){
	return htmlentities($data, ENT_QUOTES);
}
 ?>
© 2016 - 2023, All Rights are Reserved.