-2
我在編輯我的搜索欄,以便用戶可以搜索以逗號分隔的值。它調用mySQL數據庫。誰能幫忙?我相信這一定很明顯,但我是新的。謝謝!搜索欄能夠用逗號進行搜索-mySQL/php
搜索頁面:
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php include("../includes/header-home.php"); ?>
<div class="container">
<div class="col=md-12">
<p><strong>Search:</strong></p>
<form name="form1" method="post" action="search_results.php">
<p><input name="search" type="text" size="40" maxlength="50"/></p>
<p>
<input type="checkbox" name="type[]" value="1"> Medalist</p>
<p><strong>Medal:</strong>
<select name="medal">
<option disabled selected></option>
<option value="1">Medal 1</option>
<option value="2">Medal 2</option>
<option value="3">Medal 3</option>
</select>
</p>
<p><strong>State:</strong>
<select name="state">
<option disabled selected></option>
<option value="1">AL</option>
<option value="2">AK</option>
<option value="3">AZ</option>
<option value="4">AR</option>
</select>
</p>
<p><strong>Year:</strong>
<select name="year">
<option disabled selected></option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</optio>
</select>
</p>
<p><strong>Topic:</strong>
<select name="field">
<option disabled selected></option>
<option value="1">Field 1</option>
<option value="2">Field 2</option>
<option value="3">Field 3</option>
</select>
</p>
搜索結果頁:
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php
if (!isset ($_POST['search'])) {
header("Location:admin.php");
}
$search = $_POST['search'];
$type = (isset($_POST['type']) ? "AND medalist = '1'" : "");
$medal = (isset($_POST['medal']) ? "AND medal = '{$_POST['medal']}'" : "");
$state = (isset($_POST['state']) ? "AND state = '{$_POST['state']}'" : "");
$year = (isset($_POST['year']) ? "AND year = '{$_POST['year']}'" : "");
$field = (isset($_POST['field']) ? "AND field = '{$_POST['field']}'" : "");
$search_sqli = "
SELECT * FROM profiles WHERE (
first_name LIKE '%{$search}%'
OR last_name LIKE '%{$search}%'
OR first_name2 LIKE '%{$search}%'
OR last_name2 LIKE '%{$search}%'
OR last_name2 LIKE '%{$search}%'
OR city LIKE '%{$search}%'
OR agency LIKE '%{$search}%'
OR subcomponent LIKE '%{$search}%'
OR team_name LIKE '%{$search}%'
OR achievement LIKE '%{$search}%'
OR profile LIKE '%{$search}%'
OR year LIKE '%{$search}%'
)
{$type}
{$medal}
{$state}
{$year}
{$field}
";
$search_query=mysqli_query($connection, $search_sqli);
if (mysqli_num_rows($search_query) !=0) {
$search_rs=mysqli_fetch_assoc($search_query);
}
?>
<?php include("../includes/header-home.php"); ?>
<div class="container">
<div class="col=md-12">
<p>Search:</p>
<form name="form1" method="post" action="search_results.php">
<input name="search" type="text" size="40" maxlength="50"/>
<input type="submit" name="submit" value="Search" />
</form>
<br />
<p><strong>Search Results:</strong></p>
<?php if (mysqli_num_rows($search_query) !=0) {
do {
?>
<p><ul>
<li><a href="view_profile.php?profile=<?php echo urlencode($search_rs["id"]); ?>"><?php echo $search_rs['first_name']; ?> <?php echo $search_rs['last_name']; ?> <?php echo $search_rs['first_name2']; ?> <?php echo $search_rs['last_name2']; ?></a></li></ul></p>
<?php } while ($search_rs=mysqli_fetch_assoc($search_query));
} else {
echo "No results found";
}
?>
<br />
<p> <a class="btn btn-default" href="search.php" role="button">Back to search</a></p>
</div></div>
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>
如果你確實有一個用逗號分隔的值的搜索框,你應該在某處使用'explode()',但是我沒有看到它。 – developerwjk 2015-02-10 20:04:36