[英]Select all users with specific MySql request PHP
我正在用PHP編寫功能,它是一個搜索引擎。
<form action="result" method="POST"> <select name="instrument" id="instrument"> <option value="all">All instrument</option> <option value="1">Guitar</option> <option value="2">Bass</option> <option value="3">Battery</option> <option value="4">Singer</option> </select> <select name="theme" id="theme"> <option value="all">All themes</option> <option value="1">Metal</option> <option value="2">Jazz</option> <option value="3">Rock</option> <option value="4">Blues</option> </select> <input type="submit" value="search" /> </form>
我已經像這樣編碼了這個搜索引擎,並且可以正常工作
<?php
if(isset($_POST)) {
$theme = $_POST['theme'];
$instrument = $_POST['instrument'];
$req_search = $bdd->query("SELECT * FROM user,theme,instrument WHERE theme.theme_id = user.user_theme AND instrument.instrument_id = user.user_instrument AND theme_id =".$theme." AND instrument_id =".$instrument);
if($req_search->rowCount()>0) {
while($search = $req_search->fetch()) {
?>
<p><?php echo $search['user_username']; ?></p>
<?php
}
$req_search->closeCursor();
} else {
echo "Users not found";
}
}
?>
您可以找到與這些選項(沒有“ All instrument
和“ All themes
)匹配的用戶 。
一切都存儲在MySql數據庫中。
但是,當我單擊“ All themes
和“ All instrument
。 我想更新我的選擇請求。 就像我選擇“ All instrument
和“ All themes
我想向每個用戶顯示。 或者,如果我選擇“ All instrument
和Metal
,則想顯示所有正在聽金屬並彈奏任何樂器的用戶。
謝謝你的幫助 !
您可以將查詢構造為條件查詢,具體取決於$theme
和$instrument
是否具有值:
$query = "SELECT * FROM user,theme,instrument
WHERE theme.theme_id = user.user_theme
AND instrument.instrument_id = user.user_instrument".
($theme!="" ? " AND theme_id='$theme'" : "").
($instrument!="" ? " AND instrument_id='$instrument'" : "");
$req_search = $bdd->query($query);
就是說,您應該研究PDO Prepared Statements和Placeholders ,因為這將需要有人在DOM上擺弄一些MySQL注入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.