How to hold temporary values using PHP

Asked By 20 points N/A Posted on -
qa-featured

 

Hi Everyone! I have a code in php and I have a problem on how to hold temporary values before saving it in database?

SHARE
Best Answer by Jose K Vincent
Best Answer
Best Answer
Answered By 60 points N/A #103045

How to hold temporary values using PHP

qa-featured

Hi Elvis,

So many PHP users having same query. What you can do is you can save the values in the session variable before you perform any database operation. You can save the values in $_SESSION and can do any database operation that suits you. Remember in PHP you can do 200000 active connection, so that is a huge pool for any mid to high level application.

Lets have one example:

You can store the values like these:


<?php
$_SESSION["WORD"] = "ABC" ;
$_SESSION["EXCEL"] = "RED" ;


$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO Persons (FirstName, LastName)
VALUES (WORD, EXCEL)");

mysqli_close($con);
?>

Hope this will help. Thanks.

Answered By 50 points N/A #103046

How to hold temporary values using PHP

qa-featured

Hi,

You can save values temporary in a session variable or else you can add into local  variable in the current page and pass through POST method to next page until it save to database. You can pass data through url as well. Below is an example of passing data through session variable which is common and passing through url.

Example

1.Using Session

//Add data into session

$_SESSION[‘variablename’] = “Data to be insert”;

//Retrieve data from session

Session_start();

echo localvariable =  $_SESSION[‘variablename’];

 

2.Using url

<form action = ‘nextpage.php?msg1=$variable1topass&msg2=$variable2topass’ method = ‘get’>

<!—form data to be passed –>

</form>

 

You can retrieve variable1 and variable2 as below:

$newvariable1 = $_request[‘msg1’];

$newvariable2 = $_request[‘msg2’];

Hope my solution can help you.

Related Questions