Php 无法使简单的if/else语句正常工作

Php 无法使简单的if/else语句正常工作,php,mysql,variables,if-statement,Php,Mysql,Variables,If Statement,我正在数据库中搜索一个表,以便在名称与我在URL中传递的$\u GET['name']变量匹配时返回一个勾号 如果名称存在,我希望显示一个勾号,如果不存在,则显示一个叉号 目的是填写一份rota/可用性时间表 到目前为止我掌握的代码 我的桌子有三行 身份证件 个人识别码 人名 我使用的数组是 Array ( [0] => Array ( [rota_id] => 16 [person_id] => 0

我正在数据库中搜索一个表,以便在名称与我在URL中传递的$\u GET['name']变量匹配时返回一个勾号

如果名称存在,我希望显示一个勾号,如果不存在,则显示一个叉号

目的是填写一份rota/可用性时间表

到目前为止我掌握的代码

我的桌子有三行

身份证件 个人识别码 人名 我使用的数组是

Array
(
    [0] => Array
        (
            [rota_id] => 16
            [person_id] => 0
            [person_name] => Gina
        )

)
您将看到Gina存在-因此,如果?name=Gina在我的URL中,则应显示一个勾号,但?name=Fred或?name=John等在我的URL中,则应显示一个十字


问题:当名称存在时会显示勾号,但当名称不存在时不会显示十字。

此查询将从数据库中仅选择一个人

$query = "SELECT
            rota_sunday.id AS rota_id,
            rota_sunday.person_id,
            rota_sunday.person_name
        FROM rota_sunday WHERE rota_sunday.person_name = " . $_GET['name'] . "LIMIT 1"; 
try 
{ 
    $stmt = $conn->prepare($query); 
    $stmt->execute(); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to run query: " . $ex->getMessage()); 
} 
$rota_sun = $stmt->fetch(PDO::FETCH_ASSOC);
如果此人存在,$rota_sun将包含结果,如果不存在,则返回false,您将获得红十字会

   <?php if($rota_sun) { ?>
          <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
   <?php } else { ?>
          <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
   <?php } ?>
一个有效的例子:-

<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">

<?php
  error_reporting(E_ALL); //check all type of errors
  ini_set('display_errors',1); // display those if any happen
  $_GET['name'] = 'fiksun';
  $rota_sun = Array
(
    '0' => Array
        (
            'rota_id' => 16,
            'person_id' => 0,
            'person_name' => 'Gina'
        ),
    '1' => Array
        (
            'rota_id' => 16,
            'person_id' => 0,
            'person_name' => 'fiksun'
        )

)
?>
<?php foreach($rota_sun as $rota_sunday): ?>
    <?php if($rota_sunday['person_name']== $_GET['name']) { ?>
        <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
      <?php } else { ?>
        <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
  <?php } ?>
<?php endforeach; ?>

目前的问题是什么?它是否总是输入if或else?您是否检查了$rota_sunday['person_name']和$u GET['name']的值?您应该检查是否有空格和大小写。当名称存在时,会显示勾号,但当名称不存在时,不会显示十字。@Anant这正是我想要的-精确地标记名称,如果为true,则显示勾号。不匹配显示一个叉。@Anant我已将其添加到我的原始问题中-您将看到Gina存在-因此,如果?name=Gina在我的URL中,则应显示一个勾号,但?name=Fred或?name=John等在我的URL中,则应显示一个叉号。这就是我正在做的,但使用if/else语句显示一个匹配项,如果不匹配则显示另一项。但我似乎无法让它工作。问题不在于我的查询——它工作正常,并将值存储为数组。我能在垃圾堆里看到他们。如果姓名在my DB表中无法匹配,则问题是显示一个叉号。您需要显示所有人名还是其中一个?@Ben check this query$query=选择rota_sunday.id作为rota_id,rota_sunday.person_id作为person_id,rota_sunday.person_name作为rota_sunday的人名,rota_sunday在rota_sunday的位置。人名类似“$\u获取['$name'].%';是的,这个查询似乎是合法的,但是为什么$\u GET['name']不是$\u GET['name']?谢谢你的回答。。。我想我通过使用您的示例找到了问题的原因-尽管仍然无法修复-我的数组似乎只从DB返回一个结果。数组[rota_id]=>17[person_id]=>0[person_name]=>Gina,尽管有两条记录。由于您的查询,它得到了1个结果。@Ben检查我在回答中给出的查询。使用它并check@Anant谢谢-查询确实在运行,但它会在一个框中返回所有记录-只需打勾或打叉即可-当我将$\u GET['$escort']更改为$\u GET['name']时,它会运行-谢谢大家的帮助。
   <?php if($rota_sun) { ?>
          <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
   <?php } else { ?>
          <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
   <?php } ?>
<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">

<?php
  error_reporting(E_ALL); //check all type of errors
  ini_set('display_errors',1); // display those if any happen
  $_GET['name'] = 'fiksun';
  $rota_sun = Array
(
    '0' => Array
        (
            'rota_id' => 16,
            'person_id' => 0,
            'person_name' => 'Gina'
        ),
    '1' => Array
        (
            'rota_id' => 16,
            'person_id' => 0,
            'person_name' => 'fiksun'
        )

)
?>
<?php foreach($rota_sun as $rota_sunday): ?>
    <?php if($rota_sunday['person_name']== $_GET['name']) { ?>
        <span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
      <?php } else { ?>
        <span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
  <?php } ?>
<?php endforeach; ?>
$query = "SELECT rota_sunday.id AS rota_id, rota_sunday.person_id AS person_id, rota_sunday.person_name AS person_name FROM rota_sunday WHERE rota_sunday.person_name Like '%".$_GET['$name']."%'";