简体   繁体   English

将SQL表结果限制为仅选择的结果

[英]Limiting sql table results to only what is selected

I feel like I am very close to figuring this out, but just cannot close the gap. 我觉得我非常想弄清楚这一点,但无法弥补差距。 I have a list that is pulling from an SQL database and I am wanting to be able to choose one item from the list and ONLY display the info that the tables are connected to. 我有一个从SQL数据库中提取的列表,我希望能够从列表中选择一项,并且仅显示表所连接的信息。 Everything is pulling up exactly right, with the exception that ALL the items from the list are pulling instead of just the one that is selected. 一切都完全正确,但列表中的所有项目都在提取,而不仅仅是所选内容。

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

Above is the code for the form and table. 上面是表格和表格的代码。 Again, everything itself working. 同样,一切本身都在起作用。 The database is connecting and pulling up everything great. 数据库正在连接并拉动一切。 The only thing that I am trying to do is make sure that the data that is outputted is ONLY the item that is selected from the drop down list. 我要尝试做的唯一一件事就是确保输出的数据仅是从下拉列表中选择的项目。

Below is a screenshot of the list. 以下是列表的屏幕截图。 I have only 3 items in the database currently (on purpose). 我目前(故意)在数据库中只有3个项目。 So I selected Battleaxe instead of just the battleaxe line, all 3 showed up. 因此,我选择了Battleaxe而不是仅仅选择了Battleaxe系列,所有这三个都出现了。

武器清单

EDIT 1 编辑1

As requested here is the full pages code. 根据要求,这里是整页代码。 The page in it's entirety works, the database is connecting and all that. 整个页面都可以正常工作,数据库正在连接等等。 It's only that the output is not putting the 1 selected weapon. 只是输出未放入所选的1支武器。 Following some previously suggested changes stopped inputting the weapons into the table at all, so for the sake of clarity, I am posting the page code as originally listed. 遵循先前提出的一些更改之后,根本没有将武器输入到表中,因此为了清楚起见,我将发布最初列出的页面代码。

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

The results from the above code are shown in the screenshot above. 以上代码的结果显示在上面的屏幕快照中。 The drop down displays the correct weapons list as it should, but the idea is I want to be able to select one of the weapons, hit generate and it only shows the one selected weapon in the table below. 下拉列表会正确显示正确的武器列表,但我的想法是我希望能够选择其中一种武器,然后点击“生成”,并且它仅显示下表中的一种选定武器。 Right now it is showing all the weapons from the list, not only the one selected. 现在,它显示了列表中的所有武器,而不仅仅是所选的一种。

It should works: 它应该工作:

<?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;
    }
}
?>

Closing this question as new question was formulated. 随着新问题的提出,结束这个问题。 The current code 当前代码

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM