gpt4 book ai didi

php - 使用php从mysql中搜索数据

转载 作者:行者123 更新时间:2023-11-30 00:02:29 25 4
gpt4 key购买 nike

我在 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);
}

?>

任何帮助将不胜感激。

最佳答案

“非常感谢!!!它正在工作。把它作为答案,我将选择它作为最佳答案!:D”

根据要求:

你不能这样做哪里?

为您的列分配变量或选择实际的列名称。

MySQL 不知道如何“向前看”。

即:

$column = "column_name";
WHERE $column LIKE CONCAT('%',?,'%')")

WHERE column_name LIKE CONCAT('%',?,'%')")

关于php - 使用php从mysql中搜索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24902001/

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