gpt4 book ai didi

php - 只显示数据库中的特定内容

转载 作者:行者123 更新时间:2023-11-29 22:19:13 24 4
gpt4 key购买 nike

我有一些代码将遍历数据库并以表格形式显示结果(如下)。

我想使用此代码,以便我可以选择一个用户并更新表以仅显示用户名与下拉列表中的用户名匹配的信息:

    <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>

最佳答案

尝试指定选择特定用户的条件

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));

关于php - 只显示数据库中的特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30913424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com