在表中单击姓名时,如何查看个人资料?php数据库
基本上,我正在构建这个站点/门户,用户可以在其中登录、填写一些信息并将其存储(存储在数据库中)。还有管理员。当管理员登录时,它会打开一个管理员页面,其中有一个站点上所有注册用户的表。我需要做的是,当管理员按下表中的名称(或显示“查看配置文件”的按钮)时,它会将用户的电子邮件存储在$\u会话['email']中,并将用户的id存储在$\u会话['user\u id']中,然后将其重定向到配置文件概览页面(使用电子邮件和用户id)从数据库中提取有关用户的所有信息 现在的问题是。。。目前,我还不知道它如何知道在新变量中保存哪些数据,以及它如何准确地保存这些数据,因为它只是在构建表时打印数据(据我所知),之后我认为没有办法隔离行并基本上这样说“如果单击了此用户,请选择位于同一行的电子邮件及其id” 这是我的php(第一部分在页面的最顶端,第二部分是html正文中的php)在表中单击姓名时,如何查看个人资料?php数据库,php,html,database,mysqli,Php,Html,Database,Mysqli,基本上,我正在构建这个站点/门户,用户可以在其中登录、填写一些信息并将其存储(存储在数据库中)。还有管理员。当管理员登录时,它会打开一个管理员页面,其中有一个站点上所有注册用户的表。我需要做的是,当管理员按下表中的名称(或显示“查看配置文件”的按钮)时,它会将用户的电子邮件存储在$\u会话['email']中,并将用户的id存储在$\u会话['user\u id']中,然后将其重定向到配置文件概览页面(使用电子邮件和用户id)从数据库中提取有关用户的所有信息 现在的问题是。。。目前,我还不知道它
最好使用来自管理页面的get请求来配置文件页面。根据从get请求中检索到的电子邮件或任何唯一id(如注册id),您可以使用以下查询从数据库中检索有关他的所有详细信息:
如果您使用电子邮件作为唯一因素,请执行以下操作
SELECT*FROM table\u name,其中email=$\u GET['email']
和从mysqli_fetch_array
获取所有详细信息。最好使用从管理页面到配置文件页面的get请求。根据从get请求中检索到的电子邮件或任何唯一id(如注册id),您可以使用以下查询从数据库中检索有关他的所有详细信息:
如果您使用电子邮件作为唯一因素,请执行以下操作
SELECT*FROM table_name WHERE email=$\u GET['email'];
和mysqli_fetch_array
获取所有详细信息。这是我对您要求的任务的解决方案建议
工作原则:
…基于管理和客户端功能之间的明确分离(请参阅下面代码部分中标题为“文件系统结构”的段落):
在管理区域:
- 管理员登录(在admin/login.php中)。如果操作成功,则设置
$\session['adminId']
值,并将管理员重定向到客户端列表页面(admin/index.php)
- 在客户端列表页面中,验证
$\session['adminId']
值。如果无效,管理员将重定向到管理员登录页面(admin/login.php)
- 客户端列表表包含在一个表单中。每条记录都包含一个提交按钮-视图配置文件-它将相应的客户端id作为值属性
- 当管理员单击查看配置文件按钮时,表单被提交到self(例如,提交到admin/index.php)。读取提交的客户端id,设置
$\u session['clientId']
值,管理员被重定向到客户端配置文件页面(client/profile\u overview.php)
- 注销按钮重定向到admin/logout.php页面
在客户区:
- 在客户端的配置文件概述页面(client/profile\u overview.php)中,验证
$\u session['clientId']
值。如果无效,则将用户(管理员或客户端)重定向到客户端登录页面(client/login.php)。否则,$\u session['clientId']
读取值,并根据该值从数据库中提取并显示客户端详细信息
- 注销按钮重定向到client/logout.php页面
两个领域的资源:
这些文件包含在单独的文件夹中,如includes(用于需要包含的php资源-db连接、函数、错误处理程序等)、images(整体使用)等
一些建议:
- 该代码包含我自己的命名约定。原则上,在所有页面上维护已建立的约定。请参阅
- 不要从php代码/结构创建(例如输出、打印)html代码
- 将数据库查询代码(顶部)与html代码(底部)分开
- 使用面向对象的MySQLi库,而不是过程库。例如,使用fetch_数组而不是MySQLi_fetch_数组
- 不要抑制任何错误(如@operator,如@mysqli_query)。让错误被捕获和处理。有关正确的错误/异常处理,请参阅和
- 不要在页面之间发送超过id的任何内容。所有其他详细信息(电子邮件、姓名等)都将从目标页面中的db获取
- 尽可能发送POST请求
- 如果确实不需要显示,而只是为了引用值,则应隐藏表中的id列。如果根本不需要,则不应创建它们
- 您输入了一个错误:它是AD标记,而不是线程
- 请注意,AD中的th数必须与tbody中的td数相同
- 对于客户端格式化任务,请使用css类和css规则。当然,请避免使用不推荐使用的属性-例如,HTML5不支持align属性。请始终检查元素/类/规则的可用性/兼容性
- 如果您有查看配置文件按钮,那么您实际上不需要客户端名称列上的锚。不过,在您的任务群中,如果您仍然希望使用锚,那么您有两个选项。最简单的一个选项是:发送GET请求,将客户端id作为查询字符串值传递。复杂的选项:例如,您必须创建隐藏输入以保存客户端id,并使用javascript submit()函数发布相应的输入。但这意味着您的代码必须创建大量隐藏输入(数字等于num)
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>