基本上我正在构建这个站点/门户网站,用户可以在其中登录,填写一些信息并存储它(存储在数据库中)。还有管理员。当管理员登录时,会打开一个管理页面,其中包含该站点上所有注册用户的表。我需要的是,当管理员按下表格中的名称(或“查看个人资料”按钮)时,它会将用户的电子邮件存储在$ _SESSION ['email']中,并将用户的ID存储在$ _SESSION中[ 'user_id']之后,它将他重定向到个人资料概述页面,在那里它(使用电子邮件和用户ID)从数据库中提取有关用户的所有信息。
现在的问题......目前我无法理解如何知道要在新变量中保存哪些数据,以及它如何保存它,因为它只打印出数据(据我所知) )虽然它构建了表格,之后我认为没有办法隔离行,并且基本上说“如果点击了这个用户,请选择同一行的电子邮件,以及他的ID”。
这是我的php。 (第一部分位于页面的最顶部。第二部分是html正文中的php
<?php
// The user is redirected here from login.php.
session_start();// Start the session.
// If no session value is present, redirect the user:
if (!isset($_SESSION['id'])) {
exit;
}
// Print a customized message
echo "<img src=\"skcac_logo.png\" alt=\"SKCAC Logo\" width=\"300px\">";
//<p align='right'><a href=\"logout.php\">Logout</a></p>";
//connect to database
require ('mysqli_connect.php');
//make the query
$q = "SELECT id, CONCAT(lastName, ', ', firstName) AS name, email FROM
Client ORDER BY lastName ASC";
$r = @mysqli_query($dbc, $q);
//count the number of returned rows
$num = mysqli_num_rows($r);
第二部分
<?php
if ($num > 0)
{
//print how many users there are
echo "<p> There are currently $num registered participants.</p>";
//table header
echo '<div class="col col-2"></div>
<div class="col col-8 table_div"><table class="table">
<thread>
<tr>
<th scope="col" align="center">ID</th>
<th scope="col" align="center">Name</th>
<th scope="col" align="center">Email</th>
</tr>
</thread>
<tbody>';
//Fetch and print all the records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
echo
'<tr>
<td align="center">' . $row['id'] . '</td>
<td align="center"><a class="client_name" href="client_profile_overview.php">' . $row['name'] . '</a></td>
<td align="center">' . $row['email'] . '</td>
<td align="center"><button type="button" class="btn btn-default btn-sm view_profile_btn" id="view_profile_btn">View Profile</button></td>
</tr>';
if (isset($_POST['view_profile_btn']))
{
$_SESSION['participants_id'] = $row['id'];
$_SESSION['participants_name'] = $row['name'];
redirect_user('client_profile_overview.php');
}
}
//close the table and free up the resources.
echo '</tbody></table></div>
<div class="col col-2"></div>';
mysqli_free_result($r);
}
else //if no records were returned
{
//Public message
echo '<p class="error">There are currently no registered users.</p>';
//debuggin message
echo '<p>' . mysqli_error($dbc) . '<br><br>Query: ' . $q . '</p>';
}//end of if ($r) IF
?>'
答案 0 :(得分:3)
您可以使用GET方法,以便使用<a href="client_profile_overview.php?
rowid ='.$row['id'].'">View Profile</a>
而不是按钮将所选配置文件的ID重定向到client_profile_overview.php页面。
从那里,您可以执行选择查询以检索和打印必要的详细信息。
$ sql =“SELECT id,firstName,lastName,email FROM Client WHERE id ='。$ _ GET ['rowid
']。'限制1“;
答案 1 :(得分:1)
最好使用管理页面中的get请求来分析页面。根据电子邮件或任何唯一ID,例如从get请求中检索到的注册ID,您可以使用查询从数据库中检索有关他的所有详细信息
如果您使用电子邮件作为您的独特因素,那么执行
SELECT * FROM table_name WHERE email=$_GET['email'];
和mysqli_fetch_array
获取所有详细信息。
答案 2 :(得分:1)
以下是我对您提出的任务的解决方案建议。
...基于 admin 和客户端功能之间的明确分离(请参阅标题为&#34;文件系统结构和#34的段落) ; 在下面的代码部分中):
在 admin 区域:
$_SESION['adminId']
值,并将管理员重定向到客户列表页面( admin / index.php )。$_SESION['adminId']
值。如果无效,管理员将被重定向到管理员登录页面( admin / login.php )。$_SESION['clientId']
值,并将管理员重定向到客户端配置文件页面( client / profile_overview.php )。在客户端区域:
$_SESION['clientId']
值。如果无效,则将用户(admin OR客户端)重定向到客户端登录页面( client / login.php )。否则,将读取$_SESION['clientId']
值,并根据该值从数据库中提取客户端详细信息并显示。这两个领域的资源:
这些包含在单独的文件夹中,例如 includes (对于需要包含的php资源 - 数据库连接,函数,错误处理程序等), images (已使用)总体而言)等。
admin
index.php
login.php
logout.php
client
login.php
logout.php
profile_overview.php
images
skcac_logo.png
includes
connection.php
functions.php
<?php
/**
* Redirect to the given location.
*
* @param string $location Target page location.
*/
function redirect($location) {
header('Location: ' . $location);
exit();
}
<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'yourdb');
define('USERNAME', 'youruser');
define('PASSWORD', 'yourpassword');
/*
* Enable internal report functions. This enables the exception handling,
* e.g. mysqli will not throw PHP warnings anymore, but mysqli exceptions
* (mysqli_sql_exception).
*
* MYSQLI_REPORT_ERROR: Report errors from mysqli function calls.
* MYSQLI_REPORT_STRICT: Throw a mysqli_sql_exception for errors instead of warnings.
*
* @link http://php.net/manual/en/class.mysqli-driver.php
* @link http://php.net/manual/en/mysqli-driver.report-mode.php
* @link http://php.net/manual/en/mysqli.constants.php
*/
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli(HOST, USERNAME, PASSWORD, DATABASE, PORT);
<?php
require '../includes/functions.php';
session_start();
/*
* Just for testing: set the id of the logged-in admin. It should be set in the admin login page.
* @todo Remove the line.
*/
$_SESSION['adminId'] = 173;
// If no admin id is set, redirect to the admin login page.
if (!isset($_SESSION['adminId']) || empty($_SESSION['adminId'])) {
redirect('login.php');
}
// Operations performed upon form submission.
if (isset($_POST['submit'])) {
$clientId = $_POST['submit'];
// Set the client id, in order to be used in the client's profile overview page.
$_SESSION['clientId'] = $clientId;
// Redirect to the client's profile overview page.
redirect('../client/profile_overview.php');
}
require '../includes/connection.php';
$sql = 'SELECT
id,
CONCAT(lastName, ", ", firstName) AS name,
email
FROM Client
ORDER BY lastName ASC';
$result = $connection->query($sql);
$clients = $result->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo - Clients list</title>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
<style type="text/css">
body {
padding: 50px;
}
.logo {
/* ... */
}
.page-links {
margin-bottom: 30px;
}
.logout {
float: right;
}
.records-number {
margin-bottom: 30px;
}
.table-container {
/* ... */
}
.clients-list th,
.clients-list td {
/*text-align: center;*/
}
.id-col {
/* If not really needed to be displayed, then hide the *id* columns in tables. */
/* If not needed at all, then don't create any *id* columns. */
/* display: none; */
}
</style>
</head>
<body>
<img src="..images/skcac_logo.png" class="logo" alt="SKCAC Logo" width="300px">
<p class="page-links">
<a href="logout.php" class="logout">
Logout
</a>
</p>
<div class="page-content">
<?php
if ($clients) {
?>
<p class="records-number">
There are currently <?php echo count($clients); ?> registered participants.
</p>
<div class="col col-2"></div>
<div class="col col-8 table-container">
<form action="" method="post">
<table class="table clients-list">
<thead>
<tr>
<th class="id-col">ID</th>
<th>Name</th>
<th>Email</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php
foreach ($clients as $client) {
// Create variables for better usage/readability in the further cells creation codes.
$id = $client['id'];
$name = $client['name'];
$email = $client['email'];
?>
<tr>
<td class="id-col">
<?php echo $id; ?>
</td>
<td>
<?php echo $name; ?>
</td>
<td>
<?php echo $email; ?>
</td>
<td>
<!-- Notice the button value. It holds the client id to be passed to the profile overview page. -->
<button type="submit" id="viewProfileButton" name="submit" value="<?php echo $id; ?>" class="btn btn-default btn-sm btn-view-profile">
<i class="fa fa-user" aria-hidden="true"></i> View profile
</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
</div>
<?php
} else {
?>
<p class="error">
There are currently no registered clients.
</p>
<?php
}
?>
</div>
</body>
</html>
<?php
require '../includes/functions.php';
session_start();
// If no client id is set, redirect to the client login page.
if (!isset($_SESSION['clientId']) || empty($_SESSION['clientId'])) {
redirect('login.php');
}
// Read the client id set in the admin's clients list page.
$clientId = $_SESSION['clientId'];
require '../includes/connection.php';
/*
* The SQL statement to be prepared. Notice the so-called markers,
* e.g. the "?" signs. They will be replaced later with the
* corresponding values when using mysqli_stmt::bind_param.
*
* @link http://php.net/manual/en/mysqli.prepare.php
*/
$sql = 'SELECT
id,
firstName,
lastName,
email
FROM Client
WHERE id = ?
LIMIT 1';
/*
* Prepare the SQL statement for execution - ONLY ONCE.
*
* @link http://php.net/manual/en/mysqli.prepare.php
*/
$statement = $connection->prepare($sql);
/*
* Bind variables for the parameter markers (?) in the
* SQL statement that was passed to prepare(). The first
* argument of bind_param() is a string that contains one
* or more characters which specify the types for the
* corresponding bind variables.
*
* @link http://php.net/manual/en/mysqli-stmt.bind-param.php
*/
$statement->bind_param('i', $clientId);
/*
* Execute the prepared SQL statement.
* When executed any parameter markers which exist will
* automatically be replaced with the appropriate data.
*
* @link http://php.net/manual/en/mysqli-stmt.execute.php
*/
$statement->execute();
/*
* Get the result set from the prepared statement.
*
* NOTA BENE:
* Available only with mysqlnd ("MySQL Native Driver")! If this
* is not installed, then uncomment "extension=php_mysqli_mysqlnd.dll" in
* PHP config file (php.ini) and restart web server (I assume Apache) and
* mysql service. Or use the following functions instead:
* mysqli_stmt::store_result + mysqli_stmt::bind_result + mysqli_stmt::fetch.
*
* @link http://php.net/manual/en/mysqli-stmt.get-result.php
* @link https://stackoverflow.com/questions/8321096/call-to-undefined-method-mysqli-stmtget-result
*/
$result = $statement->get_result();
// Fetch data and save it into an array.
$client = $result->fetch_array(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo - Profile Overview</title>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
<style type="text/css">
body {
padding: 50px;
}
.logo {
/* ... */
}
.page-links {
margin-bottom: 30px;
}
.logout {
float: right;
}
</style>
</head>
<body>
<img src="..images/skcac_logo.png" class="logo" alt="SKCAC Logo" width="300px">
<p class="page-links">
<!-- Here you can create a link to go back to the clients list page. For this you must check if adminId is set in the SESSION variable first. -->
<a href="logout.php" class="logout">
Logout
</a>
</p>
<div class="page-content">
<?php
if ($client) {
// Create variables for better usage/readability in the further cells creation codes.
$id = $client['id'];
$firstName = $client['firstName'];
$lastName = $client['lastName'];
$email = $client['email'];
?>
<p>
The profile of the client with ID <?php echo $id; ?>.
</p>
<div class="col col-2"></div>
<div class="col col-8">
<div>
First Name: <?php echo $firstName; ?>
</div>
<div>
Last Name: <?php echo $lastName; ?>
</div>
<div>
Email: <?php echo $email; ?>
</div>
</div>
<?php
} else {
?>
<p class="error">
No client details found.
</p>
<?php
}
?>
</div>
</body>
</html>