【问题标题】:Why is my filter page not finding results within the database?为什么我的过滤器页面没有在数据库中找到结果?
【发布时间】:2017-09-05 16:22:13
【问题描述】:

我正在创建一个过滤器页面,该页面应该返回一个结果表,具体取决于从下拉列表中选择的选项。 我已经为此生成了代码,没有发现任何错误。但是,尽管它们存在于数据库中,但没有显示任何结果。

我错过了什么吗? 如果需要任何进一步的信息,请告诉我。

任何帮助将不胜感激!

    <?php

error_reporting(E_ALL);
ini_set('display_errors','1');

$search_output= "";

$link= mysqli_connect("localhost","root","");
mysqli_select_db($link,"assessment_centre_app");

if(isset($_POST['submit']))  {

$Name_FK = $_POST['Name_FK'];

$sqlCommand = mysqli_query($link, "SELECT * 
                                    FROM scores 
                                        WHERE 'Name_FK' = {'$Name_FK'}");

$query = mysql_query($sqlCommand) or die (mysql_error());

$count = mysqli_num_rows($query);   

    if($count > 1) {
        $search_output .="$count results for $searchquery";         
        while($row = mysql_fetch_array($query)) {
            $Candidate_Name_FK = $row["Candidate_Name_FK"];
            $search_output .="Item ID: $Candidate_Name_FK <br />";
                }
    } else {
        $search_output= "0 results found";

        }
    } 
    ?>      





<html>
<head>
</head>
<body>
<form action="http://localhost/scoresheet/scoresheetfilter.php" method="POST">

            <label> Assessment Day Name </label>                    
            <select name = "Name_FK">
            <?php
            $res=mysqli_query($link,"SELECT * FROM scores");
            while($row=mysqli_fetch_array($res))
            {
            ?>
            <option>
            <?php echo $row["Name_FK"];  ?>
            </option>
            <?php } ?>
            </select>
            <br>
            <input name ="myBtn" id="submit" type="submit" >            
            <br>
        </form>

<div>
<?php echo $search_output;
 ?>         
</div>
</body>
</html>

【问题讨论】:

  • 您可以尝试将 '' 放在 {} 之外吗? '{$Name_FK}'
  • 'Name_FK' 应该是反引号,而不是引号,或者什么都没有。引号用于字符串。您也对 SQL 注入持开放态度。
  • 您好刘易斯,感谢您的建议。我试了一下,但我仍然遇到同样的问题。
  • 嗨,克里斯,不幸的是,我也没有运气应用反引号。这只是作为概念证明提供的,完整版本将涵盖安全性。
  • 将此行更新为:$sqlCommand = mysqli_query($link, "SELECT * FROM scores WHERE Name_FK = '$Name_FK'");

标签: php html sql search filter


【解决方案1】:

如果它仍然不显示任何内容,请尝试这个检查用于调用另一个变量的变量

<?php

 error_reporting(E_ALL);
 ini_set('display_errors','1');

   $search_output= "";

 $link= new mysqli("localhost", "user_name", "password", "database");

if(isset($_POST['submit']))  {

    $Name_FK = $_POST['Name_FK'];

       $sqlCommand = "SELECT * FROM scores WHERE Name_FK='$Name_FK'";

      $query = mysqli_query($linnk, $sqlCommand) or die (mysqli_error());

     $count = mysqli_num_rows($query);   

 if($count > 1) {
    $search_output .="$count results for $searchquery";         
    while($row = mysqli_fetch_array($query)) {
        $Candidate_Name_FK = $row["Candidate_Name_FK"];
        $search_output .="Item ID: $Candidate_Name_FK <br />";

        echo $search_output; //echo to check result
            }
} else {
    $search_output= "0 results found";
    echo $search_output; //echo to check result
    }
} 
?> 

已编辑

变量 $搜索查询

未创建

【讨论】:

  • 嗨,JBoy,不幸的是没有运气。您还有什么可以建议的吗?
  • 但您确定您已将表单发布到此页面,因为它使用 POST
  • 不确定您的意思是什么?你能解释一下吗?
  • 我已经编辑了上面的代码以确保没有错误。但请确保将表单提交到此页面,否则它将无法正常工作。如果您对代码仍有任何疑问,请删除 if(isset['submit']{ 以查看脚本是否显示任何错误。
猜你喜欢
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多