我對PHP很新穎,並且使用此腳本來搜索我的工作數據庫。問題是,當查詢到達這個腳本,它看起來像這樣search-result.php?query=engineer+sydney
......不過,我需要尋找兩個詞在一起,並出現類似這樣search-result.php?query=engineer&sydney
與&
代替+
將+更改爲&in搜索字符串
這事我應該是試圖從搜索表單或搜索腳本本身執行操作?我已經添加了下面的搜索腳本和下面的表單。
任何幫助將是偉大的!
<div class="joblist">
<?php
$query = $_GET['query'];
$query = sanitise($query);
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM job_jobs
WHERE (`description` LIKE '%".$query."%') OR (`summary` LIKE '%".$query."%') OR (`title` LIKE '%".$query."%') OR (`location` LIKE '%".$query."%') ") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<h3 style='padding:0;margin:0;'><a href='job-view.php?query=".$results['jid']."'>".$results['title']. "</a></h3>";
echo "<i style='color:#999;'>Posted on: " . date("jS M Y", strtotime($results['dateposted']))."</i><br/>" . $results['summary'] . "<br/>";
echo "Salary: " . $results['rate'] . " | Work Type: " . $results['worktype'] . " | Location: " . $results['location'];
echo "<br/><br/>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "<h3>No Results</h3>Your search returned no results. Please try again.";
}
}
else{ // if query length is less than minimum
echo "<h3>Error</h3>The minimum length is $min_length characters. Please try again.";
}
?>
</div>
<nav class="widget-search">
<h3>Search for a Job</h3>
<form action="search-result.php" method="GET">
<button class="search-btn-widget"></button>
<input class="search-field" type="text" name="query" onblur="if(this.value=='')this.value='eg. Civil Engineer Perth';" onfocus="if(this.value=='eg. Civil Engineer Perth')this.value='';" value="eg. Civil Engineer Perth" />
</form>
</nav>
非常感謝您的幫助。 – verrucktfuchs
@verrucktfuchs是否解決了您的問題? –
不幸的不是。我嘗試過,但它似乎沒有爲我工作。不管怎麼說,還是要謝謝你! – verrucktfuchs