我在开发方面还很新,因为我之前所做的一切只是管理数据库。我已经提出了一个系统,将有三种类型的用户。
通过将用户角色管理到登录中,我成功地创建了多用户登录页面。但目前我遇到的问题是,我无法查看只有当前用户以前提交的数据。例如,我有两个用户,Ariel和Lyla。我想做的是,当Ariel登录到系统时,Ariel只能看到她提交给数据库的内容,就像目前一样,她可以看到提交的全部数据。我已经这么做了
$sql = "SELECT * FROM iirincidentmain_draft WHERE username='$_SESSION[user][username]'";但作为回报,我得到了这个错误
注意:数组到字符串的转换
我的完整代码如下
<?php
session_start();
//Checking User Logged or Not
if(empty($_SESSION['user'])){
header('location:../index.php');
}
//Restrict admin or Moderator to Access user.php page
if($_SESSION['user']['role']=='admin'){
header('location:../admin/index.php');
}
if($_SESSION['user']['role']=='management'){
header('location:../management/index.php');
}
require_once("../db.php");
?>
<div class="col-md-9 bg-white padding-2">
<h3>Reports in Draft</h3>
<div class="row margin-top-20">
<div class="col-md-12">
<div class="box-body table-responsive no-padding">
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<th>Incident Date</th>
<th>OPU Region Or Country</th>
<th>Incident Title</th>
<th>Incident Category</th>
<th>Status</th>
<th>Draft IIR</th>
<th>Edit</th>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM iir_incidentmain_draft WHERE username='$_SESSION[user][username]'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{?>
<tr>
<td><?php echo date("y-m-d", strtotime($row['incident_date'])); ?></td>
<td><?php echo $row['opus']; ?></td>
<td><?php echo $row['incident_title']; ?></td>
<td><?php echo $row['incident_category']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><a href="iir_draft.php?id=<?php echo $row['incident_id']; ?>"> <i class="fa fa-files-o"></i></a></td>
<td><a href="edit_draft.php?id=<?php echo $row['incident_id']; ?>"> <i class="fa fa-edit"></i></a></td>
<?php
}
} ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
有人能告诉我吗?
发布于 2020-01-17 02:44:07
更改这一行:
$sql = "SELECT * FROM iir_incidentmain_draft WHERE username='$_SESSION[user][username]'";
成为:
$sql = "SELECT * FROM iir_incidentmain_draft WHERE username='" . $_SESSION['user']['username'] . "'";
换句话说,您有一个错误,其中缺少会话中键的引号。当将数组值附加到字符串时(我认为)也有必要使用点表示法。因此,可能会出现字符串转换错误,这是因为解析器在嵌入外部双引号时才会看到$SESSION。
其他人可能会注意到,你应该摆脱这一切。转义用户名的一种有效方法是设置sql准备语句。
https://stackoverflow.com/questions/59780348
复制相似问题