[英]Limiting sql table results to only what is selected
我觉得我非常想弄清楚这一点,但无法弥补差距。 我有一个从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个项目。 因此,我选择了Battleaxe而不是仅仅选择了Battleaxe系列,所有这三个都出现了。
编辑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>
以上代码的结果显示在上面的屏幕快照中。 下拉列表会正确显示正确的武器列表,但我的想法是我希望能够选择其中一种武器,然后点击“生成”,并且它仅显示下表中的一种选定武器。 现在,它显示了列表中的所有武器,而不仅仅是所选的一种。
它应该工作:
<?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;
}
}
?>
随着新问题的提出,结束这个问题。 当前代码
<?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;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.