【发布时间】:2019-06-10 12:41:51
【问题描述】:
所以我最近在我的网站上创建了一个部分,该部分显示了一个从 MySQL 数据库填充的表。由于用户登录,有些记录我不想让其他用户看到,但有些用户我确实想看到。所以我设置了一个安全级别系统。到目前为止工作正常。在这一页中,我运行一个 if 语句来查看用户的安全级别是否为 4,如果有,我会根据他们的姓名运行 SELECT * 查询。所以查询是
SELECT * WHERE `assignedTo` = '$username'
理论上可行,它不会给我任何错误,并且我已打开错误报告。这是我设置的错误代码;
//Error Reporting
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
但是,我在数据库中有一条记录用于测试目的,assignedTo 字段现在包含“Example1”,当我以“Example2”登录时,我仍然可以看到分配给“Example1”的这条记录。我运行了一个 var_dump(),它显示了以下内容;
object(PDOStatement)#3 (1) {
["queryString"]=>
string(68) "SELECT * FROM `customerLeads` WHERE `assignedTo` = 'Example2' "
}
所以查询是正确的,它应该是这样运行的,但它不应该显示记录,因为assignedTo 等于“Example1”而不是“Example2”。
这是我的网页代码;
<?php
//Session Start
session_start();
//Error Reporting
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
require 'includes/conn.php';
$user_id = $_SESSION['user_id'];
$user_security = $_SESSION['user_security'];
if(!isset($_SESSION['user_id']) || !isset($_SESSION['logged_in'])){
//User not logged in. Redirect them back to the login.php page.
?>
<script type="text/javascript">
alert("You must be logged in!");
window.location.href = "login.php";
</script>
<?php
}
$user_name = $_SESSION['user_name'];
if ($user_security == 4) {
$customerList = $salesConn->query("SELECT * FROM `customerLeads` WHERE `assignedTo` = '$user_name' ");
}
if ($user_security == 0 OR 1) {
$customerList = $salesConn->query("SELECT * FROM customerLeads");
}
var_dump($customerList);
?>
然后这是我显示数据的表格;
<table>
<thead>
<tr>
<th scope="col">Full Name</th>
<th scope="col">Telephone</th>
<th scope="col">Vehicle of Interest</th>
<th scope="col">Budget</th>
<th scope="col">P/X Vehicle</th>
<th scope="col">P/X Reg</th>
<th scope="col">P/X Finance</th>
<th scope="col">P/X Settlement</th>
<th scope="col">Salesman</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<?php
while ($cRow = $customerList->fetch()) {
$firstName = $cRow['customerFirstName'];
$lastName = $cRow['customerLastName'];
$tele = $cRow['customerTel'];
$mob = $cRow['customerMob'];
$mail = $cRow['customerEmail'];
$add1 = $cRow['customerAdd1'];
$add2 = $cRow['customerAdd2'];
$add3 = $cRow['customerAdd3'];
$add4 = $cRow['customerAdd4'];
$vehInterest = $cRow['customerVeh'];
$vehChange = $cRow['customerChangeDate'];
$vehDrive = $cRow['customerDriveDate'];
$vehFinance = $cRow['customerFinance'];
$vehBudget = $cRow['customerBudget'];
$pxVeh = $cRow['customerPXVeh'];
$pxReg = $cRow['customerPXReg'];
$pxMile = $cRow['customerPXMileage'];
$pxColour = $cRow['customerPXColour'];
$pxEngine = $cRow['customerPXEngine'];
$pxTrans = $cRow['customerPXTrans'];
$pxFuel = $cRow['customerPXFuel'];
$pxPrice = $cRow['customerPXPrice'];
$pxFinance = $cRow['customerPXFinance'];
$pxSettle = $cRow['customerPXFinance'];
$salesman = $cRow['customerSalesman'];
$notes = $cRow['customerNotes'];
$status = $cRow['customerStatus'];
?>
<tr>
<td data-label="Ful Name"><?php echo $firstName." ".$lastName; ?></td>
<td data-label="Telephone"><?php echo $tele; ?></td>
<td data-label="Vehicle of Interest"><?php echo $vehInterest; ?></td>
<td data-label="Budget"><?php echo $vehBudget; ?></td>
<td data-label="P/X Vehicle"><?php echo $pxVeh; ?></td>
<td data-label="P/X Reg"><?php echo $pxReg; ?></td>
<td data-label="P/X Finance"><?php echo $pxFinance; ?></td>
<td data-label="P/X Settlement"><?php echo $pxSettle ?></td>
<td data-label="Salesman"><?php echo $salesman; ?></td>
<td data-label="Status"><?php echo $status; ?></td>
</tr>
<?php
}
?>
</tbody>
【问题讨论】:
-
当您在数据库上手动运行相同的查询时,它是否还会返回意外结果?如果是这样,您可以从调试/问题中删除所有 PHP,并将其范围缩小到 SQL。另外,请注意,您的代码对 SQL 注入是开放的。如果“安全”确实是一个问题,您需要查看准备好的语句和查询参数。
-
我已经手动在数据库上运行了查询,它运行正常,它不返回任何结果。所以它与PHP有关。嗯,我想我可能需要开始另一个查询,因为我想使用 ?在我的查询中将是一个聪明的举动。使用 BindValues 会更安全吗?
-
如果同一个查询返回两个不同的结果,则表明调试中可能有问题。也许某处存在错误的假设?尝试一些测试以在全新的页面上复制问题,包括硬编码值或使用硬编码变量中的值等。
-
啊,我发现了一个问题。所以我把 var_dump() 放在两个 IF 语句下面的位置,当我运行它时,查询是一个 SELECT * 查询。所以出于某种原因,甚至认为有两个 IF 语句,它们都不能等于同一件事。该代码将进入两个 IF 语句。由于最后的 IF 语句是一个 SELECT * ,这意味着它正在选择所有内容。我可能解释得很糟糕,但会更新我的问题以匹配
标签: php mysql pdo html-table