简体   繁体   中英

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. 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). So I selected Battleaxe instead of just the battleaxe line, all 3 showed up.

武器清单

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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