PHP Firebase Crud tutorial

Google firebase curd in PHP example tutorial. In this tutorial, you will learn how you can create crud application in PHP using firebase database with bootstrap from scratch(step by step).

Installation:
Create a folder inside xampp (php 8 version based xampp) and paste this code
composer require "kreait/firebase-php" Create index.php file and import this code:
1. Goto project setting for json file and download json file
2. https://console.firebase.google.com/u/0/project/php-firebase-oauth/settings/general
3. https://console.firebase.google.com/u/0/project/php-firebase-oauth/settings/serviceaccounts/adminsdk
4. click generate new private key and click generate key btn.
5. paste this file inside php project folder.
6. And add path here:
7. $factory = (new Factory)->withServiceAccount('php-firebase-oauth-firebase-adminsdk-sc65a-18f71c3027.json');
copy database link:
(dburl:  https://php-firebase-oauth-default-rtdb.firebaseio.com/ )
8. Goto here now: https://firebase-php.readthedocs.io/en/stable/realtime-database.html
dbcon.php
<?php
require __DIR__.'/vendor/autoload.php';
use KreaitFirebaseFactory;
$factory = (new Factory)
->withServiceAccount('php-firebase-oauth-firebase-adminsdk-sc65a-18f71c3027.json')
->withDatabaseUri('https://php-firebase-oauth-default-rtdb.firebaseio.com/');
$database = $factory->createDatabase();
?>
index.php (Insert Form)
<head>
<title>PHP_FIREBASE CRUD</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
<br/>
<div style="padding:30px">
<form method="post" action="submit.php">
<h4>Registration Form</h4><br/>
<div>
<label>Name:</label>
<input type="text" name="name" />
</div>
<div>
<label>Email:</label>
<input type="email" name="email" />
</div>
<div>
<label>Mobile:</label>
<input type="number" name="phone" />
</div>
<br/>
<button type="submit" name="submit">Submit</button>
</form>
</div>
submit.php (Data insert)
<?php
require 'dbcon.php';
if (isset($_POST['submit'])) {
   $name = $_POST['name'];
   $email = $_POST['email'];
   $phone = $_POST['phone'];
}
$postData = [
   'name' => $name,
   'email' => $email,
   'phone' => $phone
];
$postRef = $database->getReference('contacts')->push($postData);
if ($postRef) {
   echo "Data Saved Successfully !";
}
else{
  echo "Failled !";
}
index.php (Data Fetch)
<?php 
$reference = $database->getReference('contacts'); // ('tablename')
echo $reference->getValue(); // get value fetch operation
index.php (Data fetch table)
<head>
<title>PHP_FIREBASE CRUD</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
<div>
<table>
<thead>
<tr>
<th>S.No</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
require 'dbcon.php';
$reference = $database->getReference('contacts');
$fetchdata = $reference->getValue();
$i = 0;
if($fetchdata > 0){
   foreach ($fetchdata as $key => $value) { $i++; ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $value['phone']; ?></td>
<td>
<a href="edit.php?id=<?php echo $key; ?>">Edit</a>
<a href="delete.php?id=<?php echo $key; ?>">Delete</a>
</td>
</tr>
<?php   }
else{
echo "<h3>No Data Found !";
}
?>
</tbody>
</table>
</div>
<style>
td, th{
  text-align:center;
}
th{
  font-weight:900;
}
</style>
edit.php (edit form)
<head>
<title>PHP_FIREBASE CRUD</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
<br/>
<?php
require 'dbcon.php';
$id = $_GET['id'];
$postRef = $database->getReference('contacts')->getChild($id)->getValue();
?>
<div style="padding:30px">
<form method="post" action="update.php">
<h4>Registration Form</h4><br/>
<div>
<label>Name:</label>
<input type="text" name="name" value="<?php echo $postRef['name']; ?>" />
</div>
<div>
<label>Email:</label>
<input type="email" name="email" value="<?php echo $postRef['email']; ?>" />
</div>
<div>
<label>Mobile:</label>
<input type="number" name="phone" value="<?php echo $postRef['phone']; ?>" />
<input type="hidden" name="id" value="<?php echo $id; ?>">
</div>
<br/>
<button type="submit" name="submit">Update</button>
</form>
</div>
update.php:
<?php
require 'dbcon.php';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$id = $_POST['id'];
}
$postData = [
    'name' => $name,
    'email' => $email,
    'phone' => $phone
];
$table = 'contacts/'.$id;
$postRef = $database->getReference($table)->update($postData);
if ($postRef) {
  echo "Data Updated Successfully !";
}
else{
  echo "Failled !";
}
delete.php:
<?php
require 'dbcon.php';
$id = $_GET['id'];
$table = 'contacts/'.$id;
$postRef = $database->getReference($table)->remove();
if ($postRef) {
   echo "Data Deleted Successfully !";
}
else{
  echo "Failled !";
}
?>

Leave a Reply