Firebase in PHP Part-5: How to edit and update data in firebase in PHP

here, in this, we are edit and upate the data into firebase database in php.

Step 1: Create a edit.php file and paste below code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Firebase in PHP</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
    
   
    <section class="mt-5">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-body">
                            <h3>Edit data and Update using php in Firebase (Database)</h3>
                            <hr>
                            <?php 
                                include('includes/dbcon.php');
                                $token = $_GET['token'];
                                $ref = "contact/";
                                $getdata = $database->getReference($ref)->getChild($token)->getValue();
                            ?>
                            <form action="code.php" method="POST">
                                <div class="row">
                                    <input type="hidden" name="ref_token" value="<?php echo $token?>">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <input type="text" name="name" class="form-control" value="<?php echo $getdata['name']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <input type="text" name="email" class="form-control" value="<?php echo $getdata['email']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <input type="text" name="phone" class="form-control" value="<?php echo $getdata['phone']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-12 mb-2">
                                        <div class="form-group">
                                            <button type="submit" name="update_data" class="btn btn-info btn-block">Update Data</button>
                                            <hr>
                                            <a href="index.php" class="btn btn-danger btn-block">Cancel</a>
                                        </div>
                                    </div>
                                    
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>


    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

    <script src="https://www.gstatic.com/firebasejs/7.14.0/firebase-app.js"></script>
    <!-- If you enabled Analytics in your project, add the Firebase SDK for Analytics -->
    <script src="https://www.gstatic.com/firebasejs/7.14.0/firebase-analytics.js"></script>
    <!-- Add Firebase products that you want to use -->
    <script src="https://www.gstatic.com/firebasejs/7.14.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.0/firebase-firestore.js"></script>
</body>
</html>


Step 2: Create a file named called code.php and paste below code

<?php 
include('includes/dbcon.php');
session_start();

if(isset($_POST['update_data']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $token = $_POST['ref_token'];

    $data = [
        'name' => $name,
        'email' => $email,
        'phone' => $phone,
    ];

    $ref = "contact/".$token;
    $postdata = $database->getReference($ref)->update($data);

    if($postdata){
        $_SESSION['status'] = "Data Updated Successfully";
        header("Location: index.php");
    }else{
        $_SESSION['status'] = "Data Not Updated. Something Went Wrong.!";
        header("Location: index.php");
    }
}


Step 3: create a file for dbconnection - dbcon.php and paste the below code:


<?php
   require __DIR__.'/vendor/autoload.php';

   use Kreait\Firebase\Factory;
   use Kreait\Firebase\ServiceAccount;

   // This assumes that you have placed the Firebase credentials in the same directory
   // as this PHP file.
   $serviceAccount = ServiceAccount::fromJsonFile(__DIR__ . '/yourjsonfile.json');
   $firebase = (new Factory)
      ->withServiceAccount($serviceAccount)
      ->withDatabaseUri('yourdatabaseproject.firebaseio.com')
      ->create();
      
   $database = $firebase->getDatabase();
?>


© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT