2017-02-23 34 views
0

我覺得我很接近搞清楚了這一點,但就是無法縮小差距。我有一個從SQL數據庫拉列表,我希望能夠從列表中選擇一個項目,只顯示該表連接到信息。除了所有來自列表的項目正在拉動,而不僅僅是所選的項目之外,所有東西都拉得恰到好處。限制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個都出現了。

Weapon List

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

它應該工作:

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

嗯,這就是超近!但是現在我得到一個未知的列錯誤。以下是我所看到的圖像。 (我複製你貼什麼)【條款】(http://imgur.com/wdeYuWp)我想編輯說,名單不再是「技術上」顯示在下方,但它顯示有關一條線未知的列。 – Leo

+0

你有專欄戰斧嗎? –

+0

不,上面的代碼顯示沒有列。但是,「未知列」錯誤正在改變,這取決於我從下拉菜單中選擇哪個選項。所以在鏡頭中顯示的是戰斧,但我可以嘗試選擇俱樂部,而不是說列俱樂部未知。 我覺得它與WHERE子句(我的意思是,錯誤確實是這樣說的) – Leo

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