[英]search through a database using multiple keywords in php
i have a page index.php
that has three keywords on basis of which user can search a database. 我有一个页面index.php
,其中具有三个关键字,用户可以根据这些关键字搜索数据库。 code for this page is 此页面的代码是
<section id="services" class="emerald">
<div class="container">
<form class="form-horizontal" role="form" action="search.php" enctype="multipart/form-data" method="post">
<div class="row">
<div class="col-md-4 col-sm-6">
<div class="media">
<div class="media-body">
<?php
$servername = "xyz.com";
$username = "xyz";
$password = "xyz123";
$dbname = "xyz";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT treatment_type FROM treatment_type";
$result = $con->query($sql);
echo "<label for='treatment_type'>Treatment Type: </label>";
echo "<select name='treatment_type' id='treatment_type' class='form-control'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['treatment_type'] . "'>" . $row['treatment_type'] . "</option>";
}
echo "</select>";
?>
</div>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="media">
<div class="media-body">
<?php
$servername = "xyz.com";
$username = "xyz";
$password = "xyz123";
$dbname = "xyz";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT course FROM course";
$result = $con->query($sql);
echo "<label for='course'>Course/Conference Type: </label>";
echo "<select name='course' id='course' class='form-control'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['course'] . "'>" . $row['course'] . "</option>";
}
echo "</select>";
?>
</div>
</div>
</div><!--/.col-md-4-->
<div class="col-md-4 col-sm-6">
<div class="media-body">
<?php
$servername = "xyz.com";
$username = "xyz";
$password = "xyz123";
$dbname = "xyz";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT facilities FROM facilities";
$result = $con->query($sql);
echo "<label for='facilities'>Facilities: </label>";
echo "<select name='facilities' id='facilities' class='form-control'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['facilities'] . "'>" . $row['facilities'] . "</option>";
}
echo "</select>";
?>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="media-body">
<div class="col-md-8">
<input class="btn btn-primary" value="Search" type="submit" name="submit">
</div>
</div>
</div>
</div>
</form>
</div>
</section>
Code for search.php 用于search.php的代码
<?php
error_reporting(0);
$con=mysqli_connect("xyz.com","xyz","xyz123","xyz");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$treatment_type = mysqli_real_escape_string($con, $_POST['treatment_type']);
$course = mysqli_real_escape_string($con, $_POST['course']);
$facilities = mysqli_real_escape_string($con, $_POST['facilities']);
$sql1 = "SELECT * FROM office WHERE keywords LIKE '%$treatment_type' and '%$course' and '%$facilities'";
$result = mysqli_query($con, $sql1);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)) {
echo "Office name: " . $row["office_name"]. " - Location: " . $row["office_address"]. " " . $row["office_city"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($con);
?>
What i am trying to do is that when a user hits the search link, the user gets redirected to another page searchresult.php
where a list of all the available details gets displayed. 我正在尝试做的是,当用户点击搜索链接时,该用户将重定向到另一个页面searchresult.php
,其中显示所有可用详细信息的列表。 but when i run the script i am getting the value 0 results even if the value exists in the database. 但是,当我运行脚本时,即使该值存在于数据库中,我也会得到0值的结果。
Try this Query in searchresult.php
在searchresult.php
尝试此查询
$sql1 = "SELECT * FROM office WHERE keywords LIKE '%$treatment_type%'
or keywords LIKE '%$course%' or keywords LIKE '%$facilities%'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.