Sorry I'm new to these stuff but I am having a hard time about this. So I'll trying to create a php and mysql program which can view records from database server. In my form I have 2 dropdown list and 2 submit button, the first one the the list of "section" and submit button, and the second one is a list of "year level" and submit button again. If I select the section "A" and click submit button, it will display only the student's information that belongs only on section "A" and same as on "year level". I can't figure it out whenever i click the submit button, i can't view the records. I hope it made sense. Please do you have any idea to make it work? here is what I have so far--->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View</title>
</head>
<body>
<form action="view.php" method="POST">
<td> </td><label>By Section: </label>
<td> </td><select name="Section" value="<?php echo $Section; ?>">


<option value="a">A</option>


<option value="b">B</option>


<option value="c">C</option>


<option value="d">D</option>

</select>


<td> </td><input type="submit" value="GO" name="sec"/>




<td> </td><label>By Year: </label>
<select name="Year" value="<?php echo $Year; ?>">


<option value="1">First Year</option>


<option value="2">Second Year</option>


<option value="3">Third Year</option>


<option value="4">Fourth Year</option>

</select>


<td> </td><input type="submit" value="GO" name="year"/>




<?php

include('quiz1_connect.php');
mysql_connect("$server", "$user", "$pass")or die("cannot connect"); 
mysql_select_db("$db")or die("cannot select DB");

if (isset($_POST['sec']))
{
if (is_numeric($_POST['Section']))
{
$Section = $_POST['Section'];

$result = mysql_query("SELECT * FROM tblquiz1 WHERE SecName='$Section'") 
or die(mysql_error()); 



echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID:</th> <th>First Name:</th> <th>Last Name:</th> <th>Year:</th> <th>Section:</th> </tr>";


while($row = mysql_fetch_array( $result )) {


echo "<tr>";
echo '<td>' . $row['IDNo'] . '</td>';
echo '<td>' . $row['FName'] . '</td>';
echo '<td>' . $row['LName'] . '</td>';
echo '<td>' . $row['YearLvl'] . '</td>';
echo '<td>' . $row['SecName'] . '</td>';
echo "</tr>"; 
} 


echo "</table>";

}
}
?>
</form>
</body>
</html>

Dani AI

Generated

Quick summary and diagnosis: got the page working, but the original code never reached the SELECT because the form value check prevented it. The Section select uses letter values (A, B, C...), yet the handler was testing for a numeric value — that conditional will fail and skip the query. Removing that numeric test (or replacing it with proper validation) fixes the immediate problem.

Practical fixes and checks (no code pasted here): validate inputs against an allow-list (for Section: a,b,c,d; for Year: 1..4) and use the submit button names (or their presence) to decide which filter to run. Watch for simple name mismatches between the HTML name attributes and the keys you read from $_POST — those are the most common causes of “nothing happens.” Either one form with distinct submit names or two small forms will work; ’s approach is a practical option and was correct about how submit behavior works inside forms.

Security and future-proofing: do not rely on the old mysql_* extension; move to PDO or mysqli and use prepared statements with bound parameters to prevent SQL injection. At minimum, validate and sanitize every input server-side and cast numerics before using them in queries.

Small UX and debugging tips: use GET for filter forms if you want bookmarkable URLs, show a clear “no records found” message when the result set is empty, and preserve the selected option after submit so users see what they filtered. During development, enable PHP error reporting and log SQL errors so you can spot typos in column names or failed connections quickly.

Recommended Answers

All 3 Replies

You need to seperate the two options into two forms than have if statements to display the results depending on what was selected:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View</title>
</head>
<body>
<form action="view.php" method="POST">
<td> </td><label>By Section: </label>
<td> </td><select name="Section" value="<?php echo $Section; ?>">


<option value="a">A</option>


<option value="b">B</option>


<option value="c">C</option>


<option value="d">D</option>

</select>


<td> </td><input type="submit" value="GO" name="sec"/>

</form>

<form action="view.php" method="POST">

<td> </td><label>By Year: </label>
<select name="Year" value="<?php echo $Year; ?>">


<option value="1">First Year</option>


<option value="2">Second Year</option>


<option value="3">Third Year</option>


<option value="4">Fourth Year</option>

</select>


<td> </td><input type="submit" value="GO" name="year"/>

</form>




<?php

  include('quiz1_connect.php');
  mysql_connect("$server", "$user", "$pass")or die("cannot connect"); 
  mysql_select_db("$db")or die("cannot select DB");


if (isset($_POST['sec']))
{

  $Section = $_POST['Section'];

  $query = "SELECT * FROM tblquiz1 WHERE SecName='$Section'";
}

if (isset($_POST['year']))
{

  $Year = $_POST['Year'];

  $query = "SELECT * FROM tblquiz1 WHERE YearLvl='$Year'";

}

if (isset($query))
{

  $result = mysql_query($query) or die(mysql_error()); 

  
  echo "<table border='1' cellpadding='10'>";
  echo "<tr> <th>ID:</th> <th>First Name:</th> <th>Last Name:</th> <th>Year:</th> <th>Section:</th> </tr>";


  while($row = mysql_fetch_array( $result )) {


    echo "<tr>";
    echo '<td>' . $row['IDNo'] . '</td>';
    echo '<td>' . $row['FName'] . '</td>';
    echo '<td>' . $row['LName'] . '</td>';
    echo '<td>' . $row['YearLvl'] . '</td>';
    echo '<td>' . $row['SecName'] . '</td>';
    echo "</tr>"; 
  } 


  echo "</table>";


}


?>

</body>
</html>

Only submit button should be identified for one form. No matter how much submit buttons are you using within the same form, they've only submission of that form.

Try what @Glider Pilot mentioned above.

Oh. It works! Thank you so much guys! especially Sir GliderPilot, thank you. really. :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.