Skip to content Skip to sidebar Skip to footer

How To Put Mysql Table Into Session Variable And Using The Table On Next Page?

I have two PHP pages. On page1 a temporary table is created and filled with data from a mysql database. I am trying to store this table into a $_SESSION variable so that I can put

Solution 1:

On page1 a temporary table is created and filled with data from a mysql database. I am trying to store this table into a $_SESSION variable so that I can put the table onto page2.

That's impossible.

And shouldn't be used anyway.

something wrong with your design. Most likely such a table is superfluous and you don't actually need it at all.

As of the real problem behind this one - better ask another question, explaining the real life task for which you decided to use a temporary table passed between pages.

Solution 2:

Responding to your question one by one:

Error you are Getting The error that you are getting normally is the result of incorrect spelling or reference of table name, field name or any other variable in the MySQL query. In your case, it may be due to incorrect calling/storing your Session Variable. For example,

//Instead of "table", you typed "tabel". This is just an example.
$result = mysqli_query($mysqli,"SELECT * FROM table");

Share your code so that I can try picking up this error. Above is just an example.

Storing values in Session Variable is not Recommended Suppose your user fills in the form and moves on to the next phase. The data from the first phase is transferred to the second phase via Session Variable. What if the user simply closes the tab and restarts the process? Session Variable will still be set and the previous data may interfere with the new one and can produce unexpected results.

Ideal Solution It is better to store the values in JavaScript Array and then transfer to the next page by Hidden Input field. Some of the benefits of using this logic are:

  1. Fast Performance
  2. More Secure
  3. Easily Manageable

Reference Code If you are taking the values from HTML Forms, then it is very simple to have the value in POST. Using the JQuery UI selection, you can add the selected values in a JavaScript Array.

//Declare Global JavaScript Variable on Page Load. This will be at the end of <head>
$(document).ready(function() {
    window.fase1result = [];
} );

After this, on each click event where you want to add the data to be taken to the next page, use the following code to add the value to this array.

fase1result.splice(indexOf_to_add, 1, "SelectedValue");

To understand .splice better, click here.

One selection, e.g. clicking on a Div or link, add the value to a fase1result and on submit add the array value to Input Hidden by using the following:

Add a Javascript Function on form's onsubmit.

<form id="myForm" method="POST" action="fase2.php" onsubmit="return fase1Values()">

Add <input type="hideen" name="fase1values_input" id="fase1values_id"> in the form.

Below is the JavaScript onsubmit function just before </body>.

functionfase1Values() {
    $( '#fase1values_id' ).val( JSON.stringify(fase1result) );
}

Note that JSON.stringify is required in order to set the Array as an input value.

$decode_fase1result = json_decode( $_POST['fase1values_input'] );

Now you have transferred the fase 1 selection data using an Array from Page 1 to Page 2 without storing data in any temporary table.

Hope this answers your question and solves your problem as well.

Post a Comment for "How To Put Mysql Table Into Session Variable And Using The Table On Next Page?"