我有一些代码将通过数据库并以表格形式显示结果(如下)。
我想使用此代码,以便我可以选择一个用户并让表更新只显示用户名与下拉列表中的一个匹配的信息:
<div class="form-group">
<label class="control-label" for="teamid">User</label>
<?php
$sql = mysqli_query($connection, 'SELECT id, name FROM users');
echo "<select class='form-control' name='userid' id='userid'>";
while($row = mysqli_fetch_array($sql)){
echo "<option value='".$row["id"]."'>".$row["name"]."</option>";
}
echo "</select>";
?>
</div>
原始代码:
<table class= "table table-striped table-bordered table-hover">
<tbody>
<th>User Name</td>
<th>Badge Name</th>
<th>Badge Level</th>
</tr>
</tr>
<?php
$query = mysqli_query($connection, 'SELECT ub.id, users.name, individualbadges.badgename, ub.level
FROM userbadges ub
INNER JOIN users ON users.id = ub.user_id
INNER JOIN individualbadges ON individualbadges.id = ub.badge_id') or die(mysqli_error($connection));
$i=0;
while($fetch = mysqli_fetch_array($query))
{
if($i%2==0) $class = 'even'; else $class = 'odd';
echo'<tr class="'.$class.'">
<th id="'.$fetch['id'].'" key="name"><span>'.$fetch['name'].'</span></th>
<td id="'.$fetch['id'].'" key="BadgeName"><span>'.$fetch['badgename'].'</span></td>
<td class="xedit" id="'.$fetch['id'].'" key="level" data-type="text" data-title="New Badge Level (0,1,2,3)"><span>'.$fetch['level'].'</span></td>
</tr>';
}
?>
</tbody>
</table>
答案 0 :(得分:0)
尝试通过
指定选择特定用户的条件WHERE
username
=$name
$query = mysqli_query($connection, "SELECT ub.id, users.name, individualbadges.badgename, ub.level
FROM userbadges ub
INNER JOIN users ON users.id = ub.user_id
INNER JOIN individualbadges ON individualbadges.id = ub.badge_id where `username`='$name'") or die(mysqli_error($connection));
答案 1 :(得分:0)
您需要执行以下操作
user_id
user_id
$_REQUEST
醇>
$ user_id = $ _REQUEST ['userid'];
$ query = mysqli_query($ connection,'SELECT ub.id,users.name, individualbadges.badgename,ub.level 来自userbadges ub INNER JOIN用户ON users.id = ub.user_id INNER JOIN individualbadges ON individualbadges.id = ub.badge_id where users.id ='。 $ user_id)或死(mysqli_error($ connection));
已编辑的代码
将您的sql分配给PHP变量
$ sql ='SELECT ub.id,users.name, individualbadges.badgename,ub.level 来自userbadges ub INNER JOIN用户ON users.id = ub.user_id INNER JOIN individualbadges ON individualbadges.id = ub.badge_id';
检查是否设置了$ user_id
if(isset($user_id))
{
$sql .= "WHERE users.id = '. $user_id;
}
$query = mysqli_query($connection, $sql) or die(mysqli_error($connection));
所以,你的整个代码看起来像这样:
$user_id = $_REQUEST['userid'];
$sql = 'SELECT ub.id, users.name, individualbadges.badgename, ub.level FROM userbadges ub INNER JOIN users ON users.id = ub.user_id INNER JOIN individualbadges ON individualbadges.id = ub.badge_id';
if(isset($user_id))
{
$sql .= "WHERE users.id = '. $user_id;
}
$query = mysqli_query($connection, $sql) or die(mysqli_error($connection));