如何将MySQL表放入会话变量并使用下一页的表格? [英] How to put MySQL table into session variable and using the table on next page?

查看:239
本文介绍了如何将MySQL表放入会话变量并使用下一页的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个PHP页面。在第1页上,创建了一个临时表,并用mysql数据库中的数据填充。我试图将这个表存储到$ _SESSION变量中,以便我可以将表放到page2上。

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.

现在这是我的方法:

这是第1页上代码的(部分):

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;

这是第2页上的代码:

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>";

不幸的是,到目前为止,这些脚本在第2页上给我一个错误。此时,第2页上表的回显只是为了测试并验证表是否实际传递。稍后我希望能够使用MySQL查询进一步向表中添加数据。希望你能帮助我。

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

第2页的第32行是:

while($row = mysqli_fetch_array($table))

为了更好地解释我的问题,我发布了另一个可以找到的问题这里:
修改MySQL不同页面上的表格,包含HTML表格的分数

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

推荐答案

逐一回答您的问题:

您获得的错误


您正常获得的错误是拼写错误或引用错误的结果表名,字段名或MySQL中的任何其他变量查询。在您的情况下,可能是由于调用/存储会话变量不正确。例如,

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.

理想解决方案


最好将值存储在JavaScript Array中,然后通过Hidden Input字段传输到下一页。使用此逻辑的一些好处是:

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. 快速性能

  2. 更安全

  3. 易于管理

参考代码


如果从HTML表单中获取值,则在POST中获取值非常简单。使用JQuery UI选择,您可以在JavaScript数组中添加所选值。

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");

要更好地了解 .splice 点击这里

一个选择,例如单击Div或链接,将值添加到 fase1result ,然后在提交时使用以下内容将数组值添加到Input Hidden:

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:

在表单的onsubmit上添加一个Javascript函数。

Add a Javascript Function on form's onsubmit.

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

添加< input type =hideenname =fase1values_inputid =fase1values_id> 在表单中。

以下是JavaScript onsubmit 功能就在< / body> 之前。

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

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

请注意 JSON.stringify 是将Array设置为输入值所必需的。

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

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

现在您已经使用第1页到第2页的数据传输了fase 1选择数据而没有存储数据在任何临时表中。

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.

这篇关于如何将MySQL表放入会话变量并使用下一页的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆