我正在處理複選框搜索。我是PHP新手。如果有人能幫助我,我將不勝感激。基本上用戶檢查所需的選項,點擊提交併顯示匹配複選框選項的結果。複選框的值爲男性爲M,女性爲女性,與MYSQL表中的數據相匹配。PHP MYSQL複選框搜索
因此,如果用戶選中複選框'Male'(參見下面的代碼),則會返回MYSQL表中'Male'值爲'M'的所有數據。並且任何未勾選的複選框都應該被忽略,因爲用戶只對選項'男'是'M'(真)感興趣。
總結我只需要進行搜索,以考慮已選中的複選框並回顯哪些用戶已通過PHP選擇(更像篩選)。任何幫助表示讚賞。由於
表看起來是這樣的:
id name address gender race
-------------------------------------------
1. 1 Lee NY M French
2. 2 James LA M Colombian
3. 3 Kashi JAPAN F Japanese
和我有這樣的複選框的一種形式:
<form action="shortlist.php" method="post">
<label for="sel1">Gender:</label>
Male:<input name="keyword[]" type="checkbox" value="M" />
Female:<input name="keyword[]" type="checkbox" value="F" />
<button name = "myBtn" type="submit" class="btn btn- default">Search</button>
</form>
SQL:
$sql="SELECT name, address,gender,race FROM talent1 WHERE gender = $gender'";
我猜想呼應像這樣:
echo "<table width='100%'>\n";
//if (mysqli_num_rows($result) > 0){
//$row=mysqli_fetch_assoc($result);
//$muchneededid = $row["talent_id"];
while ($row=mysqli_fetch_assoc($result))
echo'<tr>'."\n";
echo '<tr>';
echo '<th>Name</th>';
echo '<th>Address</th>';
echo '<th>Gender</th>';
echo '</tr>';
echo "<td>{$row["name"]}</td>\n" . "<td>{$row["address"]}</td>\n" . "<td>{$row["gender"]}</td>\n";
echo '</tr>'."\n";
echo "</table>\n";
}
else
{
echo "0 results";
}
mysqli_close($conn);
}
在您的html中有_no_複選框。你的意思是'select-options'? – Jeff
但基本上它會像'如果複選框'm'然後添加到sql'或'GENDER ='M'''' – Jeff
不好意思,我粘貼了錯誤的代碼。 – struppy