gpt4 book ai didi

php - 基于从 ajax 调用返回的获取变量构建查询

转载 作者:行者123 更新时间:2023-11-29 07:17:34 25 4
gpt4 key购买 nike

<分区>

我有一段代码,它可以正常工作,但不符合我的要求。我有一个有两个选择框的表单,当用户更改每个 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>

这是我的 get 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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com