首页 > 解决方案 > Can't view records based on selected drop down values from two drop downs

问题描述

I want to show a record from database based on the selected drop down value. I have a page which shows a drop down having column names and another drop down which shows the rows of that selected column from first drop down. After that there is a search button which shows all the records based on those two drop down menus i.e. the selected column name and selected row of that column. Then the search button shows a table having all the columns and shows that specific result. Problem is I don't know how to use that drop down ids in the select query. Here is my code:

<form id="searchform" action="view.php" method="post" enctype="multipart/form-data" >
<select id="select1" onChange="check(this);">
  <option selected value="" disabled="disabled">Select an Option</option> 
  <option value="all">Select All</option>   
  <option value="names" >Names</option>
  <option value="courses" >Courses</option>
</select>
<select id="select2" disabled="disabled">
  <option>Select an option</option>
  <?php 
$sql="SELECT DISTINCT names FROM table ";       
$result = mysqli_query($sql);
while ($row = mysql_fetch_array($result)) {    
    echo "<option  class='names' value=' " . $row['names'] ."'>" . $row['names'] ."</option>";
    }
?>
<?php 
$sql="SELECT DISTINCT courses FROM table ";        
$result = mysqli_query($sql);
while ($row = mysql_fetch_array($result)) {  
    echo "<option  class='courses' value=' " . $row['courses'] ."'>" . $row['courses'] ."</option>";
    }
?>
</select>
<input type="submit" name="submit" value="Search" />
</form>

Now on view.php page

<body>
<?php
$count=1;
include("connection.php");
$select1=isset($_POST['select1']);
$select2=isset($_POST['select2']);
$result=mysql_query("SELECT names,courses FROM table WHERE names='.$_REQUEST['select2'].'");

echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Names</th> <th>Courses</th></tr>";
while ($row=mysql_fetch_array($result)){?>
    <tr>  
<td align="center" ><?php echo $row["names"]; ?></td>
<td align="center" ><?php echo $row["courses"]; ?></td>
</tr>

<?php $count++; } ?>

 </table>
</body>
</html>

标签: phphtmlsql

解决方案


推荐阅读