How to put MySQL table into session variable and using the table on next page?

ぐ巨炮叔叔 提交于 2019-12-25 01:09:27

问题


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 the table onto page2.

Right now this has been my approach:

This is (part) of the code on page1:

ob_start();
session_start();

  //Select data from temporary table
$result = mysqli_query($mysqli,"SELECT * FROM table");


//store table into session variable
$_SESSION['fase1result'] = $result;

This is the code on page2:

ob_start();
session_start();

$table = $_SESSION['fase1result'];

echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>ProductName</th>
<th>Fase1</th>
</tr>";

while($row = mysqli_fetch_array($table))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "<td>" . $row['Fase1'] . "</td>";
echo "</tr>";
}
echo "</table>";

Unfortunately, up until now these scripts return me an error on page2. At this moment, the echoing of the table on page2 is just to test and verify that the table is actually passed on. At a later moment I want to be able to use MySQL queries to further add data to the table. Hope you could help me.

UPDATE:
Error that I'm getting is:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in domain/page2.php on line 32

With line 32 in page2 being:

while($row = mysqli_fetch_array($table))

To better explain my question, I have posted another question which can be found here: Modifying MySQL table on different pages with scores from a HTML form


回答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.




回答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>.

function fase1Values() {
    $( '#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.



来源:https://stackoverflow.com/questions/23128169/how-to-put-mysql-table-into-session-variable-and-using-the-table-on-next-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!