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

时间:2016-09-08 16:11:32

标签: php mysqli dropdown

好吧,我刚把它改成了$ _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>";
 ?>

2 个答案:

答案 0 :(得分:0)

不要将所有细节都放在选项值中。只需将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";
}

答案 1 :(得分:0)

处理数组时,您可以使用 $ 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>
}