我有一個頁面,它有一個連接到MySql數據庫的文本框,它用於搜索使用其姓氏的人的記錄。使用文本框和兩個下拉菜單在PHP頁面上搜索和過濾MySql結果
爲了使頁面更有用,我添加了2個下拉菜單。一個菜單用於紀念地點,另一個用於衝突,如第一次世界大戰,第二次世界大戰等。我想使用下拉菜單作爲搜索結果的過濾器。
的下拉菜單需要與文本獨立工作,並且還有記錄給出結果:
- 姓
- 姓+紀念
- 姓+衝突
- 紀念
- 紀念+衝突
- 衝突
我對形式的代碼是:
<div class="memorialphp">
<form action='./memorial.php' method='get'>
<!-- $surname or 'surname' -->
<input type='text' name='term' placeholder="Search...">
<!-- $memorial or 'memorial' -->
<select name="memorialsearch">
<option value=" ">All Memorials</option>
<option value="Hinckley War Memorial">Hinckley War Memorial</option>
<option value="Burbage War Memorial">Burbage War Memorial</option>
<option value="Barwell War Memorial">Barwell War Memorial</option>
<option value="Earl Shilton War Memorial">Earl Shilton War Memorial</option>
</select>
<!-- $conflictsort or 'conflictsort' -->
<select name="confictsearch">
<option value="">All Conflicts</option>
<option value="1">World War I</option>
<option value="2">World War II</option>
<option value="3">20th Century Wars</option>
</select>
</form>
</div>
我對PHP的代碼是:
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM search WHERE surname LIKE '%".$term."%' ORDER BY surname, fullname, conflictsort";
$r_query = mysql_query($sql);
if(mysql_num_rows($r_query)){
while ($row = mysql_fetch_array($r_query)){
$id = $row['id'];
$initial = $row['initial'];
$surname = $row['surname'];
$fullname = $row['fullname'];
$dob = $row['dob'];
$born = $row['born'];
$service = $row['service'];
$enlisteddate = $row['enlisteddate'];
$enlisted = $row['enlisted'];
$number = $row['number'];
$rank = $row['rank'];
$batt = $row['batt'];
$unit = $row['unit'];
$section = $row['section'];
$died = $row['died'];
$death = $row['death'];
$memorial = $row['memorial'];
$conflict = $row['conflict'];
$conflictsort = $row['conflictsort'];
$biography = $row['biography'];
$link = $row['link'];
echo"<h4>$surname</h4>
<b>Name:</b> $fullname - <b>Rank:</b> $rank ($number) - <b>Regiment:</b> $batt $unit $section
<br /><b>Birth:</b> $dob $born
<br /><b>Enlisted:</b> $service - $enlisteddate $enlisted
<br /><b>Died:</b> $died $death
<br /><b>Memorial:</b> Shown as $initial$surname at $memorial
<br /><b>Confilct:</b> $conflict
<br /><b><a href='$link'>$biography</a></b>
<br /><br /><br />";
}
}else{
echo "<br /><br /><br /><br /><br /><p><b>No records, please try again...</b></p>";
}
}
?>
請注意,我是一個總的初學者,理想的解決辦法讓你改變代碼併發布解釋如何改變以及如何改變它的工作。
謝謝你幫@xzoert 我必須保持mysqli_num_rows [代碼] 如果($哪裏){ \t $ SQL = 「SELECT * FROM搜索WHERE 1」 $哪裏。 \t $ r_query = $ db-> query($ sql); \t \t \t \t 如果(mysqli_num_rows($ r_query)){ \t而($行= mysqli_fetch_array($ r_query)){ \t \t $ ID = $行[ 'ID']; [/ code] – GrahamUK33
對不起,這條消息很混亂,我不能把代碼放在外面。 – GrahamUK33
是的,查詢後的代碼應該與您已有的相同(除了使用mysqli_xxx代替mysql_xxx)。 – xzoert