【发布时间】:2016-08-18 15:45:57
【问题描述】:
我已经完成了从正面匹配字符的高级搜索。我希望它也应该与单词的中间和结尾匹配,或者与数据库中的任何地方匹配。我应该怎么知道我的高级搜索代码在这里 PHP 代码............
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn=mysql_connect($servername,$username,$password);
if (!$conn) {
die('Could not connect: ' . mysqli_error($conn));
}
$in=$_GET['q'];
if(!ctype_alnum($in)){
echo "Data Error";
exit;
}
mysql_select_db('firstdb');
$sql="select name, id , age, sex from name where name like '$in%' or sex like '$in%' or age like '$in%' or id like '$in%'";
$display=mysql_query($sql,$conn);
echo "<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while( $row = mysql_fetch_assoc( $display ) ){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['sex'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
JS代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
function user(str){
if(str.length==0)
{
document.getElementById("userhint").innerHTML = "";
return;
}
else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("userhint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "advanceSearch.php?q="+str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<input type="text" onkeyup="user(this.value);" name="username" />
</form>
<br />
<div id="userhint"><b>User info will be listed here...</b></div>
</body>
</html>
【问题讨论】:
-
您能否更清楚地解释您要做什么?
-
@parveen 当我在 Text 中写入任何字符时,它应该获取包含该字符的所有记录,而与字符位置无关,但我的上述查询仅从开始匹配
标签: javascript php mysql sql ajax