1

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

Community
  • 1
  • 1
NvdB31
  • 302
  • 1
  • 3
  • 13
  • Sorry to forget that. I have updated my question. – NvdB31 Apr 17 '14 at 08:34
  • check $result have the records ?? – Haseeb Apr 17 '14 at 08:37
  • `$result = mysqli_query($mysqli,"SELECT * FROM table") or die(mysqli_error($mysqli));` what do you see? – CodeBird Apr 17 '14 at 08:37
  • mysqli_fetch_array(result,resulttype); Your syntax is not good – caps lock Apr 17 '14 at 08:39
  • 1
    The issue isn't here, the issue is because of his query not running, either because $mysqli isn't the good link to the database or because the query syntax is wrong. – CodeBird Apr 17 '14 at 08:40
  • Added that code. Doesn't give an error. Also, I've echoed the table on page1 and whole table is correctly showed. The problem should be somewhere in the passing the table to the new page I guess. – NvdB31 Apr 17 '14 at 08:42
  • ah you can't store a mysqli result in a session!!!!! – CodeBird Apr 17 '14 at 08:43
  • How should I do it then? I would like to keep the temporary table so that I can use and change the table with MySQL queries on page2. Is it an idea to use a persistent db connection for that? – NvdB31 Apr 17 '14 at 08:44
  • Like you were trying to do in the other question you asked yesterday... – CodeBird Apr 17 '14 at 08:45
  • Well, I guess if I store the table data in a php array I cannot use MySQL queries anymore to change that data, can I? – NvdB31 Apr 17 '14 at 08:47

2 Answers2

0

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • To explain my design I have posted another question. It can be found here: http://stackoverflow.com/questions/23130150/modifying-mysql-table-on-different-pages-with-scores-from-a-html-form Thanks in advance! – NvdB31 Apr 17 '14 at 10:04
0

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.