从下拉菜单中获取相同ID的整行


get the whole row of the same ID from drop down menu

好了,我只是把它改成$_POST,现在它工作了。我不确定这是否是快捷方法。至少它现在起作用了。如果你想帮我,可以帮我缩减代码。由于

<?php
 $conn = new mysqli('localhost', 'root', 'jared17', 'hbadb') 
 or die ('Cannot connect to db');
 $result = $conn->query("select * from english");
 echo "<html>";
 echo "<body>";
 echo "<form method = POST>";
 echo "<select name = 'Students'>";
 while ($row = $result->fetch_assoc()) {
          $LRN = $row['LRN'];
          $Last = $row['Last_Name']; 
          $First = $row['First_Name'];
          $Lvl = $row['Level'];
          $Q1 = $row['Q1'];
          $Q2 = $row['Q2'];
          $Q3 = $row['Q3'];
          $Q4 = $row['Q4'];
          $Final = $row['FINAL'];
          echo '<option value="'.$LRN.'|'.$Last.', '.$First.'|'.$Lvl.'|'.$Q1.'|'.$Q2.'|'.$Q3.'|'.$Q4.'|'.     $Final.'">'.$Last.', '.$First.'</option>';
 }
 echo "</select>";
 echo "<input type='submit' name='submit' value='Show'>";
 echo "</form>";
 $show = $_POST['Students'];
        $show_explode = explode('|', $show);
    echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
    echo "<tr><td>". $show_explode[0]."</td><td>". $show_explode[1]."</td><td>". $show_explode[2]."</td><td>". $show_explode[3]."</td><td>". $show_explode[4]."</td><td>". $show_explode[5]."</td><td>". $show_explode[6]."</td><td>". $show_explode[7]."</td></tr>";
 echo "</table>";


 echo "</body>";
 echo "</html>";
 ?>

不要把所有的细节都放在选项值中。只需在值中输入ID。

echo "<select name = 'Students'>";
while ($row = $result->fetch_assoc()) {
    $LRN = $row['LRN'];
    $Last = $row['Last_Name']; 
    $First = $row['First_Name'];
    echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
 }
 echo "</select>";

然后在提交表单时在数据库中查找。

if (isset($_POST['Students'])) {
    $lrn = $_POST['Students'];
    $stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?");
    $stmt->bind_param('i', $lrn);
    $stmt->execute();
    $stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final);
    $stmt->fetch();
    echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
    echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table";
}

在处理Array时可以使用$foreach来减少代码。这里的代码是

if(isset($_POST['submit'])){
// after post a form ur code goes here
$show = $_POST['Students']; $show_explode = explode('|', $show);
 echo "<table><tr>
<th>LRN</th>
<th>Name</th>
<th>Level</th>
<th>Q1</th>
<th>Q2</th>
<th>Q3</th>
<th>Q4</th>
<th>Final</th>
</tr>"; 
echo "<tr>";
foreach($show_explode as $value){
echo "<td>".$value."</td>";
}
echo "</tr></table>
}