【问题标题】:building a query based on get variables returned from an ajax call [duplicate]基于从ajax调用返回的get变量构建查询[重复]
【发布时间】:2020-02-13 11:17:28
【问题描述】:

我有这段代码可以正常工作,但不是我想要的。我有一个表单,它有两个选择框,当用户更改每个 ajax 调用的值时,页面会更新与所选选项匹配的记录。

这个位工作正常。

但是,当用户通过更改选择选项过滤记录但想要通过在两个选择框中选择“全部”来返回以显示所有记录(因此重置选项)时,什么都没有显示?我有一个 if 查询,我认为它可以根据 $query 是否为空来设置 $sql 的值。

这是我的 html/php 页面

<html>
<head>
<script type="text/javascript"> 
function showUser() 
{ 
var users = document.getElementById('instructor').value;
var sex = document.getElementById('club').value;

  if (users=="" && sex=="")
  {
    document.getElementById("txtHint").innerHTML="";
     return;
  }
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get-user.php?student_instructor="+users+"&club="+sex,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="student_instructor" id="instructor" onChange="showUser()">
<option value="">Select a person:</option>
<option value="Clive Double">Clive Double</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>

<select name="club" id="club" onchange="showUser()">
<option value="">Male/Female:</option>
<option value="">All</option>
<option value="sennen">Sennen</option>
<option value="marazion">Marazion</option>
</select>
</form>
<br />
<div id="txtHint">

<b>Person info will be listed here.</b>

</div>
</body>
</html>

这是我获取 user.php 的代码

<?php
include_once("includes.php");
$count = "";
$query = "";
#print_r($_GET);
foreach($_GET as $field => $value)
{
#echo "field = ".$field."<br />value = ".$value."<br />";

if($value != "")
{

    if($count > 0)
    {
        $query .= " AND ";
    }

    $query .= $field." = '".$value."'";
    $count++;
}
}

//ORDER BY SQL TO GO HERE


$where  = '';

if( !empty ( $query ) ) 
{
$where = ' WHERE '  . $query;
$sql = "SELECT * FROM tbl_register".$where;
}
else
{
$sql = "SELECT * FROM tbl_register";
}


#echo "<p>".$sql."</p>";
$result = mysqli_query($conn,$sql);
$numrows = mysqli_num_rows($result); ?>

<div class='col-sm-12 day-heading'>
<div class="row">
    <div class="col-sm-2">Forename</div>
    <div class="col-sm-2">Surname</div>
    <div class="col-sm-2">Club</div>
    <div class="col-sm-3">Instructor</div>
    <div class="col-sm-2">Date</div>
    <div class="col-sm-1">Age</div>         
</div>
</div>

<?php
while($row = $result->fetch_assoc())
{
$id = $row['id'];
$firstname = $row['student_forename'];
$surname = $row['student_surname'];
$age = $row['student_age'];
$club = $row['club'];
$instructor = $row['student_instructor'];
$date = $row['date_awarded'];
$grade = $row['student_grade'];
if($i % 2 == 1)
{
    $math = "odd-row";
}
else
{
    $math = "even-row";
}?>

<div class="col-sm-12 <?php echo $math?>">
    <div class="row">
        <div class="col-sm-2"><?php echo $firstname?></div>
        <div class="col-sm-2"><?php echo $surname?></div>
        <div class="col-sm-2"><?php echo $club?></div>
        <div class="col-sm-3"><?php echo $instructor?></div>
        <div class="col-sm-2"><?php echo $date?></div>
        <div class="col-sm-1"><?php echo $age?></div>           
    </div>
</div><?php
$i++;
}?>

当两个选择框都设置为全部时,我希望基本上有一个重置选项,然后显示所有记录,目前不是。

这是我认为可以工作的代码,但遗憾的是没有

if( !empty ( $query ) ) 
{
    $where = ' WHERE '  . $query;
    $sql = "SELECT * FROM tbl_register".$where;
}
else
{
    $sql = "SELECT * FROM tbl_register";
}

谁能看到我做错了什么?了解我可能很简单:D

【问题讨论】:

  • 即使你得到这个工作,它完全开放SQL注入。您可能想改变您正在采取的方法。
  • 发送ajax时检查js中users的值,有可能是空的。在 PHP 后端,您可以检查值,即用户不应为空。
  • 嗨 @david 是的,肯定需要解决 sql 注入问题,现在我已经对其他位进行了排序:)谢谢

标签: php mysql sql ajax


【解决方案1】:

您每次都在 for 循环内将 $query 变量重置为 ''。


include_once("includes.php");
$count = "";
$query = "";
print_r($_GET);
foreach($_GET as $field => $value)
{
    echo "field = ".$field."<br />value = ".$value."<br />";
    //$query = " WHERE ";  //Commented this.

    if($value != "")
    {

        if($count > 0)
        {
            $query .= " AND ";
        }

        $query .= $field." = ".$value;
        $count++;
    }
}

//Added a New Variable
$where  = '';
if( !empty ( $query ) ) {
    $where = ' WHERE '  . $query;
}
$sql = "SELECT * FROM tbl_register".$where;
echo "<p>".$sql."</p>";

【讨论】:

  • 感谢您的帮助,非常感谢!你有没有机会告诉我如何在没有 where 子句的情况下显示表中的所有数据库记录?听起来应该很容易,但我又在苦苦挣扎:(我已经用新代码更新了我的原始帖子:)谢谢!
猜你喜欢
  • 2012-03-28
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 2013-06-24
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多