我在php中进行搜索功能时遇到问题。我正在尝试更改要搜索的列,使用和html下拉框。它也应该使用占位符(%?%),以便我可以进行全局搜索。当我用字母' b'搜索任何内容时,它只显示所有记录(所有记录,当它只显示带有字母' b'的记录时)。
代码如下:
HTML(请注意,这不是一个已编辑的版本,问题似乎在PHP文件中):
<html lang="en" class=" js csstransforms3d csstransitions">
<head>
<script>
function showUser() {
var querycolumn = document.getElementById('querycolumn').value;
var query = document.getElementById('query').value;
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","contactsearching.php?querycolumn="+querycolumn+"&query="+query,true);
xmlhttp.send();
}
</script>
<script>
function userdata(str) {
if (str=="") {
document.getElementById("userdetail").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("userdetail").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","searchingforcontact.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<section class="flexform" style="display:inline-block;">
<div id="txtHint" style="padding-left:30px;padding-top:10px; padding-bottom:20px;"><b>The Contacts Which Are In The Contact Group Chosen Shall Be Displayed Here:</b></div>
<form>
<label style="clear:both;"><strong>Searching Parameter:</strong></label>
<select name="querycolumn" id="querycolumn" style="float:left;">
<option value="" selected disabled>Please Choose A Parameter To Search By</option>
<option value="CustomerID">Customer ID</option>
<option value="CustomerBusinessName">Business Name</option>
<option value="ContactPerson">Contact Person</option>
<option value="Department">Department</option>
<option value="OccupationalTitle">Occupational Title</option>
<option value="EmailAddress">Email Address</option>
<option value="PhoneNumber">Phone Number</option>
<option value="EXT">EXT</option>
<option value="FaxNumber">Fax Number</option>
<option value="BusinessAddress">Business Address</option>
<option value="BusinessCity">Business City</option>
<option value="BusinessState">Business State</option>
<option value="BusinessZipCode">Business Zip Code</option>
<option value="BusinessCountry">Business Country</option>
<option value="PostalAddress">Postal Address</option>
<option value="PostalCity">Postal City</option>
<option value="Comment">Comment</option>
</select>
<br></br>
<label style="clear:both;"><strong>Value Searching For:</strong></label><input type="text" name="query" style="float:left;" id="query" />
<input type="button" onclick='showUser()' value="Initiate Searching Sequence" style="float:left;"/>
<br></br>
</form>
</section>
</body></html>
PHP文件:
<?php
$querycolumn = $_GET['querycolumn'];
$query = $_GET['query'];
$con=mysqli_connect("localhost","user","password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<table border='4'>
<tr>
<th>Customer ID</th>
<th>Customer Business Name</th>
<th>Contact Person</th>
<th>Department</th>
<th>Phone Number</th>
<th>Email Address</th>
<th>Comment</th>
</tr>";
$stmt=$con->prepare("SELECT CustomerID, CustomerBusinessName, ContactPerson, Department, PhoneNumber, EmailAddress, Comment FROM `Contacts` WHERE ? LIKE CONCAT('%',?,'%')");
$stmt->bind_param("ss", $querycolumn, $query);
$stmt->execute();
$stmt->bind_result($col1, $col2, $col3, $col4, $col5, $col6, $col7);
while ($stmt->fetch()) {
printf ("<tr> \n <td><a href onclick='userdata($col1)'>$col1</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col2</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col3</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col4</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col5</a></td> \n <td><a href='#' onclick='userdata($col1)'>$col6</a></td> <td><a href='#' onclick='userdata($col1)'>$col7</a></td> \n \n </tr>", $col1, $col1, $col1, $col2, $col1, $col3, $col1, $col4, $col1, $col5, $col1, $col6, $col1, $col7);
}
?>
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
&#34;非常感谢!它正在工作。把它作为答案,我将其选为最佳! :d&#34;
根据要求:
您无法执行此操作WHERE ?
为列指定变量或选择实际的列名称。
MySQL并不知道如何向前看&#34;。
I.e。:
$column = "column_name";
WHERE $column LIKE CONCAT('%',?,'%')")
或
WHERE column_name LIKE CONCAT('%',?,'%')")