我觉得我非常接近于解决这个问题,但却无法缩小差距。我有一个从SQL数据库中提取的列表,我希望能够从列表中选择一个项目,并且只显示表所连接的信息。除了列表中的所有项目都是拉动而不是仅选择的项目之外,所有内容都完全正确地拉起来。
<form action="#" method="post">
<table class="table">
<thead>Martial Weapon Name</thead>
<tr>
<th>
<select name="Choosen">
<?php
echo'<option>Select Weapon</option>';
//Check if at least one row is found
if($result->num_rows >0){
//Loop through results
while($row = $result->fetch_assoc()){
//Display weapon info
$output = $row['weapon_name'];
echo '<option>'.$output.'</option>';
}
}
?>
</select>
</th>
</tr>
</table>
<input class="btn btn-default" type="submit" name="submit" value="Generate">
<h3>Weapon</h3>
<table class="table table-striped">
<tr>
<th>Weapon Name</th>
<th>Weapon Type</th>
<th>Damage</th>
</tr>
<?php
$choose= "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial";
$result = $mysqli->query($choose) or die($mysqli->error.__LINE__);
if(isset($_POST['submit'])){
$selected_weapon = $_POST['Choosen'];
while($list = $result->fetch_assoc()){
//Display weapon
$show ='<tr>';
$show .='<td>'.$list['weapon_name'].'</td>';
$show .='<td>'.$list['weapon_type'].'</td>';
$show .='<td>'.$list['weapon_damage'].'</td>';
$show .='</tr>';
//Echo output
echo $show;
}
}
?>
</form>
以上是表单和表格的代码。一切,一切都在起作用。数据库正在连接并提升一切。我要做的唯一事情是确保输出的数据只是从下拉列表中选择的项目。
以下是该列表的屏幕截图。我目前在数据库中只有3个项目(故意)。所以我选择了战斧而不仅仅是战斧线,所有3个都出现了。
编辑1
这里要求的是完整页面代码。它的整个页面工作,数据库连接和所有这些。只是输出没有放入1个选定的武器。根据之前建议的一些更改,停止将武器输入到表格中,所以为了清楚起见,我发布了最初列出的页面代码。
<?php
include('includes/database.php'); ?>
<?php
//Create the select query
$query = "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial
ORDER BY weapon_name";
//Get results of query
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>App Test | Weapons</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css"></style>
</head>
<body>
<div class="site-wrapper">
<div class="site-wrapper-inner">
<div class="cover-container">
<div class="masthead clearfix">
<div class="inner">
<h3 class="masthead-brand">Cover</h3>
<nav>
<ul class="nav masthead-nav">
<li><a href="index.php">Front Page</a></li>
<li class="active"><a href="weapons.php">Weapons</a></li>
<li><a href="armor.php">Armor</a></li>
<li><a href="consumables.php">Consumables</a></li>
</ul>
</nav>
</div>
</div>
<form action="#" method="post">
<table class="table">
<thead>Martial Weapon Name</thead>
<tr>
<th>
<select name="Choosen">
<?php
echo'<option>Select Weapon</option>';
//Check if at least one row is found
if($result->num_rows >0){
//Loop through results
while($row = $result->fetch_assoc()){
//Display weapon info
$output = $row['weapon_name'];
echo '<option>'.$output.'</option>';
}
}
?>
</select>
</th>
</tr>
</table>
<input class="btn btn-default" type="submit" name="submit" value="Generate">
<h3>Weapon</h3>
<table class="table table-striped">
<tr>
<th>Weapon Name</th>
<th>Weapon Type</th>
<th>Damage</th>
</tr>
<?php
$choose= "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial";
$result = $mysqli->query($choose) or die($mysqli->error.__LINE__);
if(isset($_POST['submit'])){
$selected_weapon = $_POST['Choosen'];
while($list = $result->fetch_assoc()){
//Display weapon
$show ='<tr>';
$show .='<td>'.$list['weapon_name'].'</td>';
$show .='<td>'.$list['weapon_type'].'</td>';
$show .='<td>'.$list['weapon_damage'].'</td>';
$show .='</tr>';
//Echo output
echo $show;
}
}
?>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
以上代码的结果显示在上面的屏幕截图中。下拉列表显示正确的武器列表,但我的想法是我希望能够选择其中一种武器,点击生成,它只显示下表中选定的一种武器。现在它显示了列表中的所有武器,而不仅仅是所选的武器。
答案 0 :(得分:0)
它应该有效:
<?php
if (isset($_POST['submit'])) {
$selected_weapon = $_POST['Choosen'];
$choose = "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial WHERE weapon_types_martial.weapon_name = " . $selected_weapon;
$result = $mysqli->query($choose) or die($mysqli->error . __LINE__);
foreach ($result->fetch_assoc() as $item) {
//Display weapon
$show = '<tr>';
$show .= '<td>' . $item['weapon_name'] . '</td>';
$show .= '<td>' . $item['weapon_type'] . '</td>';
$show .= '<td>' . $item['weapon_damage'] . '</td>';
$show .= '</tr>';
//Echo output
echo $show;
}
}
?>