PHP Temporary Data Storage Example

In this post we have learn how to PHP temporary data storage example. We store temporary data using session in PHP. Session in super global variable in PHP.

PHP Temporary Data Storage Example

data.php

In this page we have create store data form in session in php.

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
 <head>
  <title>how to store data in session in php</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
 </head>
 <body>

  <div class="container">
   <div class="row">
    <div class="col-md-2"></div>
    <div class="col-md-8" style="margin-top:50px">
     <form action="data_process.php" method="post">

      <input type="hidden" name="id" id="id" value="<?php if(isset($_SESSION['update_data'])){echo $_SESSION['update_data']['id'];} ?>">
      Name
      <input type="text" name="name" id="name" class="form-control" value="<?php if(isset($_SESSION['update_data'])){echo $_SESSION['update_data']['name'];} ?>">
      <br /><br />
      Email
     <input type="text" name="email" id="email" class="form-control" value="<?php if(isset($_SESSION['update_data'])){echo $_SESSION['update_data']['email'];} ?>">
     <br /><br />
     <input type="submit" class="btn btn-primary" value="<?php echo (isset($_SESSION['update_data']))? "Update" : "Submit"; ?>">
  </form>
</div>
<div class="col-md-2"></div>
</div>
</div>

</body>
</html>

data_process.php

<?php session_start();

 if(!empty($_POST['id'])|| $_POST['id'] == '0')
 {
  extract($_POST);
  $_SESSION['data'][$id] = array("name" =>$name, "email" =>$email);
   unset($_SESSION['update_data']);
   header("location:dataview.php");
 }
else if(isset($_GET['id']) && isset($_GET['name']))
{
 extract ($_GET);
 $_SESSION["update_data"] = array("id"=>$id,"name"=>$name,"email"=>$email);
 header("location:data.php");
}
else if(isset($_GET['id']))
{
 $id = $_GET['id'];
 unset($_SESSION['data'][$id]);
 header("location:dataview.php");
}
else if(!empty($_POST))
{
 extract($_POST);
 $_SESSION['data'][] = array("name" =>$name, "email" =>$email);
 header("location:dataview.php");
}
else
{
 header("location:data.php");
}

?>

dataview.php

Read Also :  Laravel 8 One to One Relationship Example

In this page we have display Temporary data storage in php.

<?php session_start();
?>
<!DOCTYPE html>
<html lang="en">
 <head>
  <title>how to store data in session in php</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
 </head>
 <body>

  <div class="container">
   <div class="row">
    <div class="col-md-9">
     <table class="table table-dark table-hover mt-5">
      <thead>
       <tr>
        <th>No</th>
        <th>Name</th>
        <th>Email</th>
        <th>Delete</th>
        <th>Edit</th>
        <th>Save</th>
       </tr>
      </thead>
      <tbody>
   <?php
    if(isset($_SESSION['data'])){
    $no = 0;
    foreach($_SESSION['data'] as $id=>$val)
    {
      $no++;
      echo '<tr>
        <td>'.$no.'
        <td>'.$val["name"].'
        <td>'.$val["email"].'
        <td class="text-center"><a href="data_process.php?id='.$id.'" class="text-danger"><i class="fa fa-trash" ></i></a>
        <td><a href="data_process.php?id='.$id.'&name='.$val["name"].'&email='.$val["email"].'" class="text-warning"><i class="fa fa-pencil" aria-hidden="true"></i></a>
        <td><a href="datasave.php?id='.$id.'&name='.$val["name"].'&email='.$val["email"].'"><i class="fa fa-save" aria-hidden="true"></i></a>
       </tr>';
      }
    }
  ?>
 </tbody>
</table>
</div>
 <div class="col-md-1">
  <a href="data.php" class="btn btn-primary mt-5"><i class="fa fa-plus" aria-hidden="true"></i></a>
 </div>
 <div class="col-md-2">
 <form action="datasave.php" method="post">
  <input type="hidden" name="savealldata">
  <input type="submit" class="btn btn-primary mt-5" value="Save All">
</form>
</div>
</div>

</div>

</body>
</html>

datasave.php

<?php session_start();

  $link = mysqli_connect("localhost","root","","learn");
  if(!empty($_GET))
   { 
    extract($_GET);

    $q = "insert into temporarydata (name,email)
    values('$name','$email')";

    mysqli_query($link,$q);

    unset($_SESSION['update_data']);
    header("location:dataview.php");
   }
   else if(!empty($_POST))
   {
   foreach($_SESSION['data'] as $data)
   { 
  $name = $data['name'];
  $email = $data['email'];

  $q = "insert into temporarydata (name,email)
  values('$name','$email')";

  mysqli_query($link,$q);
 }
 unset($_SESSION['data']);
 header("location:dataview.php");
}
 else
{
  header("location:dataview.php");
}

?>

SQL

--
-- Database: `learn`
--

-- --------------------------------------------------------

--
-- Table structure for table `temporarydata`
--

CREATE TABLE `temporarydata` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `temporarydata`
--
ALTER TABLE `temporarydata`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `temporarydata`
--
ALTER TABLE `temporarydata`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;