jaredpianist jaredpianist - 2 months ago 7
PHP Question

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

Okay I just changed it to $_POST and it's now working. I'm not sure if this is the shortcut method. At least it's working now. You can help me shrink the code if you want to help me. thanks

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

Answer

Don't put all the details in the option value like that. Just put the ID in the value.

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

Then look it up in the database when the form is submitted.

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";
}
Comments