我在这里努力了解$ _GET的isset逻辑测试的逻辑。这实际上是练习的答案。
所以这是这一部分:
if(isset($_GET['status']) && in_array($_GET['status'], $options)){
$sql .= " WHERE status = ?";
$execute = array($_GET['status']);
}
我将不胜感激,如果答案是//内嵌,非常感谢。
1.)据我所知,我们正试图查看状态是否为空,但为什么两次$选项也是?
2.)为什么是$ sql分裂?为什么不把$ sql带出来,如果已经用“WHERE status =?”
而PHP页面的其余部分是在这里:
<?php # Exercise solution 1 $employees = ''; $execute = array(); $options = array(0,1); $db = new PDO('mysql:host=localhost;dbname=eshop;charset=utf8', 'root', ''); $sql = "SELECT firstName,lastName FROM employees"; if(isset($_GET['status']) && in_array($_GET['status'], $options)){ $sql .= " WHERE status = ?"; $execute = array($_GET['status']); } $query = $db->prepare($sql); $query->setFetchMode(PDO::FETCH_OBJ); $query->execute($execute); $result = $query->fetchAll(); foreach($result as $row){ $employees .= "{$row->firstName} {$row->lastName}<br/>"; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Exercise solution 1</title> </head> <body> <strong>Employees list</strong> <a href="1.php">All</a> <a href="1.php?status=1">Status Active</a> <a href="1.php?status=0">Status Not Active</a> <br/><br/> <div class="result-list"><?= $employees; ?></div> </body> </html>
@FuzzyTree:不,它不。这是一个彻头彻尾的语法错误。我猜它应该是'isset()' –
既然你永远不能确定你从客户端得到了什么数据,你必须首先检查你实际发送的一些数据是否被发送:'if(isset($ _ GET ['状态']))'只有那样你才能真正使用它。 – arkascha
我也有一个错误,那就是为什么即时在这里完全困惑,这是从我们的练习答案。 – erezT