好的,我想知道如何在generate unique $_SESSION name or ID
文件中set it to each table row
和members.php
,所以当我点击此文件中的任意行时,它会重定向到另一个名为{{1的文件根据该行(在我的代码中为'member'的详细信息)从MySQL读取详细信息并打印出来。
details.php
<?php session_start();?>
<html>
<table>
<?php
include 'connect.php';/*make connection to MySQL*/
$stmt=$mysqlConnect->query('SELECT name, phone FROM members');
foreach($stmt as $row)
{
$name = $row['name'];
$_SESSION['name'] = $name;/*HELP: this is where I am strugling*/
/*the value of $_SESSION['name'] is changes each time in loop.*/
/*So, it works only for last row of table named 'members' */
/*in MySQL database.*/
?>
<tr onclick="window.document.location='details.php';">
<td><?php echo $row['name']?></td>
<td><?php echo $row['phone']?></td>
<?php
}/*end of froeach function*/
mysqlConnect=null;/*disconnect mysql*/
?>
</tr>
答案 0 :(得分:2)
你可以用$_SESSION['name']
制作一个数组,但它不会解决你的问题。每个会话只有一组会话信息(每个浏览器,而不是每个选项卡),因此在打开详细信息时,您仍然需要通过在请求中传递一些信息(get / querystring或发布信息)来了解哪一个被点击了。
使用该信息,您可以在会话中再次设置正确的名称,但这可能会导致副作用:在新选项卡中打开成员A时,会话名称将设置为A.然后您打开成员B其他新选项卡和会话名称设置为B.因为会话在浏览器中的所有选项卡之间共享,刷新成员A的详细信息现在会突然显示成员B的详细信息。
所以最好只在URL中传递名称,并根据该名称显示详细信息。或者,您可以post
信息,但为此您需要更多的javascript和/或html表单,这两者都会使事情变得比需要的更复杂。
因此,您可以在单击时将名称(或ID)传递到网址,而不是设置会话信息或发布数据。请注意,即使名称中有特殊字符,也会添加urlencode
以使网址有效。
<html>
<table>
<?php
include 'connect.php';/*make connection to MySQL*/
$stmt=$mysqlConnect->query('SELECT name, phone FROM members');
foreach($stmt as $row)
{
?>
<tr onclick="window.document.location='details.php?name=<?php echo urlencode($row['name'])?>';">
<td><?php echo $row['name']?></td>
<td><?php echo $row['phone']?></td>
<?php
}/*end of foreach function*/
mysqlConnect = null;/*disconnect mysql*/
?>
</tr>
在另一页中,您可以从$_GET
超全局读取值。通过一些额外的检查,您的页面可能如下所示。我做了一些补充,使数据库阅读更安全。我以为你在使用MySQLi。如果不是(PDO或自定义类),您可能需要进行一些更改。
<?php
include "connect.php";
// Check, if no name was given, someone visited the page directly.
// Redirect to the member list in that case.
if (!isset($_GET['name'])) {
header('Location: members.php');
exit;
}
$name = $_GET['name'];
// Use a prepared statement. This is safer, otherwise people could secretly
// enter pieces of malicious SQL into the 'name' querystring.
$stmt = $mysqlConnect->prepare(
"SELECT
name, address, mail, phone, car, credit, workDay
FROM members WHERE name = ?");
$stmt->bind_param('s', Sname);
$result = $stmt->execute();
// You can bind result parameters too. This will fetch each row directly
// into these variables.
$stmt->bind_result($name, $address, $mail, $phone, $car, $credit, $workDay);
while ($stmt->fetch()) {
echo $name;
echo $address;
echo $mail;
echo $phone;
echo $car;
echo $credit;
echo $workDay;
}
?>
答案 1 :(得分:0)
我理解您的问题和更好的解决方案避免使用会话并使用GET将值传递给details.php
<html> <table> <?php foreach($stmt as $row) { $name = $row['name']; ?> <tr onclick="window.document.location='details.php?name=<?php echo $name; ?>';"> <td><?php echo $row['name']?></td> <td><?php echo $row['phone']?></td> <?php }/*end of froeach function*/ mysqlConnect=null;/*disconnect mysql*/ ?> </tr>
然后是details.php
$name = $_GET['name'] /* Use $_GET instead of session */