我在mysql中有一个数据库表cav。
我需要在两个关键字的基础上使用php获取结果,这两个关键字将在两列中搜索即。 'caseno'和'year'。
在用户表单中,我提供了两个输入字段,即'keyword'和'keyword1'。
仅当关键字和关键字1与数据库中“caseno”和“year”(单行)列中已存在的值完全匹配时,才希望显示结果。
例如,如果用户将684作为关键字而将2007作为keyword1查询,则应获取行的数据,其中caseno列的值为684,而year列的值为2007。
我的问题是假设我将684作为关键字并且在关键字1中没有任何内容,即使它然后显示包含684的行,如果我将684作为关键字而2010年作为keyword1,即使它然后它显示包含684的行。
希望显示“caseno”为684(用户定义的关键字)和“year”为2007(用户定义的关键字)的行数据。
试过以下代码: -
<?php
$db_host="";
$db_user="";
$db_pass="";
$db_name="";
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
if($db->connect_error){
die("Connection failed: " . $db->connect_error);
}
?>
<?php
if(!empty($_POST))
{
$aKeyword = explode(" ", $_POST['keyword']);
$bKeyword = explode(" ", $_POST['keyword1']);
$query ="SELECT * FROM cav WHERE caseno like '%" . $aKeyword[0] . "%' AND
year like '%".$bkeyword[0]."%'";
$result = $db->query($query);
if(mysqli_num_rows($result) > 0) {
$row_count=0;
echo '<br><br><br><br><br>
<center><table class="table-1 mt-2 table-responsive table border="1"
table-bordered"><TABLE BORDER=2 BORDERCOLOR=BLACK font size="2">
<tr>
<th>S.No.</th>
<th>Case Type</th>
<th>Case No.</th>
<th>Year</th>
<th>Petitioner Name</th>
<th>Respondent Name</th>
<th>First Coram</th>
<th>Second Coram</th>
<th>Third Coram</th>
<th>Written By</th>
</tr></center> ';
While($row = $result->fetch_assoc()) {
$row_count++;
echo "<tr><td> ".$row_count." </td><td>". $row['casetype'] . "</td><td>".
$row['caseno'] . "</td><td>". $row['year'] . "</td><td>".
$row['petitioner'] . "</td><td>". $row['respondent'] . "</td><td>".
$row['firstcoram'] . "</td><td>". $row['secondcoram'] . "</td><td>".
$row['thirdcoram'] . "</td><td>". $row['writtenbycoram'] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "<br>Result Found: NONE";
}
}
?>
html form is
<input type="text" name="keyword" autocomplete "off">
<input type="text" name="keyword1" autocomplete "off">
<input type="submit" value="Search"><FORM><INPUT Type="button"
VALUE="Back" onClick="history.go(-1);return true;">
希望仅在两列的值(即'caseno'和'year')与用户给出的关键字完全匹配时才显示结果。如果用户只投放684或仅投放2007或684和2010或784和2007,则会显示“未找到结果”
我认为这里的代码可以很容易地解决您的问题,因为我发现您只是缺少一些要点,以使其按您的需要工作。我还使其余代码更好地管理
$html = '';
if(!empty($_POST))
{
$aKeyword = explode(" ", $_POST['keyword']);
$bKeyword = explode(" ", $_POST['keyword1']);
$query = 'SELECT * FROM cav WHERE caseno LIKE "%'.$aKeyword[0].'%" OR caseno LIKE "%'.$bKeyword[0].'%"';
$result = $db->query($query);
if(mysqli_num_rows($result) > 0) {
$row_count=0;
$html .= '<br><br><br><br><br>';
$html .= '<center><table class="table-1 mt-2 table-responsive table border="1"
table-bordered"><table border="2" bordercolor="black" font-size="2">';
$html .= '<tr>
<th>S.No.</th>
<th>Case Type</th>
<th>Case No.</th>
<th>Year</th>
<th>Petitioner Name</th>
<th>Respondent Name</th>
<th>First Coram</th>
<th>Second Coram</th>
<th>Third Coram</th>
<th>Written By</th>';
$html .= '</tr></center> ';
While($row = $result->fetch_assoc()) {
$row_count++;
$html .= '<tr><td> '.$row_count.' </td><td>'. $row['casetype'] . '</td><td>'.
$row['caseno'] . '</td><td>'. $row['year'] . '</td><td>'.
$row['petitioner'] . '</td><td>'. $row['respondent'] . '</td><td>'.
$row['firstcoram'] . '</td><td>'. $row['secondcoram'] . '</td><td>'.
$row['thirdcoram'] . '</td><td>'. $row['writtenbycoram'] . '</td>
</tr>';
}
$html .= '</table>';
}
else {
$html .= '<br>No results found';
}
}
echo $html;
您的问题是使用AND而不是使用OR