- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 PDO PHP 处理多 WHERE 子句时遇到问题。我的 Ajax 代码发送两个变量,一个用于实时搜索的输入文本,一个用于下拉菜单。这是我的代码。
Ajax代码:index.php
<p style="text-align: center">Enter your search here: <input type="text" id="search" name="search" placeholder="Enter your search here">
Select education level: <select id="edulevel">
<option value="PHD">PHD</option>
<option value="MASTER">MASTER</option>
<option value="DEGREE">DEGREE</option></select></p>
<div id="contentBox" class="login, reminder" style="margin:0px auto; width:95%; overflow-y: auto; height:304px;">
<div id="result" class="login"></div>
<script type="text/javascript">
/*
setInterval(function(){
//alert('Refreshing database');
$("#result").load("res.php", "update=true").fadeIn("slow").text("Refreshing Database");
}, 10000);
*/
function update() {
$.ajax({
url: 'userres.php',
dataType: 'text',
success: function(data) {
if (parseInt(data) == 0) {
$("#result").css({ color: "red" }).text("offline");
} else {
$("#result").css({ color: "green" }).text("online");
}
}
}); // properly end the ajax() invocation
}
function ajaxSearchUpdater(){
$("#result").show();
var x = $("#search").val();
var y = $("#edulevel").val();
$.ajax({
type:'POST',
url:'userres.php',
data:'q='+x+'&e='+y,
cache:false,
success:function(data){
$("#result").html(data)
}
});
}
$(document).ready(function(e) {
ajaxSearchUpdater(); // fires on document.ready
$("#search").keyup(function() {
ajaxSearchUpdater(); // your function call
});
$("#edulevel").click(function() {
ajaxSearchUpdater(); // your function call
});
});
</script>
对于我的 php 和数据库代码:userres.php
//print_r($_GET);
$q=$_POST['q'];
if(isset($_POST['e'])){
$e=$_POST['e'];
echo $q;
echo $e;
}
$query="SELECT *
FROM viewlibrary
WHERE
studentname LIKE :q OR
matricno LIKE :q OR
title LIKE :q OR
education_level LIKE :q OR
programme LIKE :q OR
serialno LIKE :q OR
education_level = :e
ORDER BY studentname ASC";
$stmt = $db->prepare($query);
$stmt->bindValue(':q','%'.$q.'%');
$stmt->bindValue(':e',$e, PDO::PARAM_STR);
$stmt->execute();
$a = 0;
if($stmt->rowCount() > 0){
$r=$stmt->fetchAll();
echo "<table class='tablesorter' id='myTable' style='width:97%; table-border: 1'>";
echo "<thead>";
echo "<tr>";
echo "<th>No.</th>";
echo "<th>No.Matric</th>";
echo "<th>Name</th>";
echo "<th>Programme</th>";
echo "<th>Title</th>";
echo "<th>Education Level</th>";
echo "<th>Serial Number</th>";
echo "<th>Availability</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($r as $row){
echo "<tr align='center'><td>". ($a+1) ."</td><td>". $row['matricno'] ."</td><td>". $row['studentname'] ."</td><td>". $row['programme'] ."</td><td>". $row['title'] ."</td><td>". $row['education_level'] ."</td><td>". $row['serialno'] ."</td><td>". $row['bavailable'] ."</td></tr>";
$a++;
//echo $row['education_level'];
}
echo "</tbody>";
echo "</table>";
}
else{
echo "<p align='center'>Nothing to show you :( I am really sorry for this T_T </p>";
}
?>
这里的问题是我想让我的表格显示仅具有合适教育水平的数据列表,即“学位”、“硕士”、“博士”,这取决于用户的下拉选择。我确实回显了来自 ajax 的变量,它显示了我单击的内容,但我似乎无法找到一种方法来处理这些查询。大多数 WHERE 条件用于实时搜索,我想知道我应该如何为我的下拉列表设置条件。
最佳答案
我认为您应该按用户选择的 education_level 进行搜索。
SELECT *
FROM viewlibrary
WHERE
education_level = :e AND
(studentname LIKE :q1 OR
matricno LIKE :q2 OR
title LIKE :q3 OR
education_level LIKE :q4 OR
programme LIKE :q5 OR
serialno LIKE :q6)
ORDER BY studentname ASC
也不要尝试在单个 SQL 语句中两次使用相同的命名参数,例如
<?php
$sql = 'SELECT * FROM some_table WHERE some_value > :value OR some_value < :value';
$stmt = $dbh->prepare($sql);
$stmt->execute( array( ':value' => 3 ) );
?>
这将不返回任何行,也不返回任何错误——您必须且仅使用每个参数一次。显然,由于可移植性问题,这是预期的行为(根据此错误报告:http://bugs.php.net/bug.php?id=33886)。
关于php - 如何使用来自 Ajax 变量的 PHP PDO 处理许多 WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38567285/
我试图从一些 sql 查询中获得一些额外的性能,这些查询在一个字段上有一个 where 子句,它是一个非唯一的非聚集索引,它也是表 A 中的一个外键。该外键是主键在表 B 上,是聚集索引。 我想知道的
当包含在 FOR 中时,应该如何编写此 WMIC 命令脚本中的命令? wmic service where (name="themes" and state="running") get 下面的代码不
请帮我理解如何订购 对over子句的影响。我已经阅读了 msdn 和一本书,但仍然误解了。 假设我们有这样的查询: SELECT Count(OrderID) over(Partition By Ye
参见如下SQL语句: SELECT datediff("d", MAX(invoice.date), Now) As Date_Diff , MAX(invoice.date) AS ma
不知何故,对我来说构建这样的查询有点困难:给我所有链接名称不为空的导航条目 $query = $this->db->get_where('navigation',array('linkname'!==
我一直在寻找这个,但没有发现任何特别的东西。 是否可以有一个像 ALL IN 一样的 SQL 查询?为了更好地解释,这是一个表结构。 Orders table OrderItem table (hav
SELECT DISTINCT Campaign_id FROM Impressions WHERE Date BETWEEN '2015-03-01' AND '2015-03-31' ; 上述查询
我尝试在 MyBatis 中遵循 if 子句并得到以下异常请帮助我确定这里的问题.. public class Student{ private Integer studId; private Str
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我尝试在 MyBatis 中遵循 if 子句并得到以下异常请帮助我确定这里的问题.. public class Student{ private Integer studId; private Str
是否可以用 where in 子句做这样的事情,我需要使用 where in 查询以下数据。 select * FROM instructor AS i INNER JOIN teaches AS t
嗨,我怎样才能让这个查询工作。我想要一个关于 where 子句的条件,如果 @BACHNUMB = '',那么 WHERE 是 (h.sopnumbe = @SOPNUMBE) 否则 WHERE 是
我在 MVC3 项目中工作。我浏览了一段时间并尝试了几个示例,但无法正常工作。 我需要从 OrderForm 表中获取记录列表,其 DeptID 在我已经获得的另一个列表中。 我知道我需要使用 Con
select * from staff LEFT JOIN servicereservation on servicereservation.snic = staff.snic where servi
我正在尝试使用 MySQL 创建带有“WITH”子句的 View WITH authorRating(aname, rating) AS SELECT aname, AVG(quantity)
我正在尝试使用 MySQL 创建触发器,但遇到错误。限制是:用户不得对他或她同时销售的商品出价。 Create Trigger before_insert_bid Before Insert on B
我正在尝试在 PostgreSql 的 WHERE IN 子句中使用 split_part,如下所示。这里 Objcode 是 small int 类型,objection 可能像 1374,824,
这可能很简单,只是我太厚了 - 我试图阻止保留的元素在记录中被拾取,但只有当库存大于 0 时,我不知道该怎么做除非 "....WHERE blah blah AND (reserved = 0 OR
我总结了两个表中两列的行,即如下所示: SUM( tableA.age ) + sum( tableB.age) as 'Total Ages' 但在某些情况下,A表的结果为空,而B表的结果则不是。在
我写了一个查询,从出生日期字段开始计算出一个人的年龄,然后使用 AS age 创建一个年龄字段。 我的问题是,是否可以再次匹配那个年龄字段? 像这样, SELECT `candidates`.`can
我是一名优秀的程序员,十分优秀!