0

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>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
DevJ
  • 27
  • 1
  • 9
  • `WHERE names='".$select2']."'")` – mplungjan May 14 '18 at 06:32
  • Give me the link of the duplicate question – DevJ May 14 '18 at 07:11
  • The link given above for the duplicate answer is not the solution to my problem – DevJ May 14 '18 at 07:11
  • Why not? You ask how to create a select statement from a variable. Your use of variable shows you do not know how to concatenate the variable to the select statement. Please update your question with what your expected and actual result is. Your question is not clear enough. I will re-open it, but so far I still believe it is a duplicate of https://stackoverflow.com/questions/15703608/php-mysql-query-where-x-variable – mplungjan May 14 '18 at 07:39
  • This question don't even have dropdown menu – DevJ May 14 '18 at 07:43
  • What does that mean? Where do I or the duplicate mention a dropdown menu? Please be more descriptive if the dupe is not answering your question. – mplungjan May 14 '18 at 07:49
  • As far as I can see the issue is the invalid PHP code `$select2=isset($_POST['select2']); $result=mysql_query("SELECT names,courses FROM table WHERE names='.$_REQUEST['select2'].'");` which to my mind should be `$select2=$_POST['select2']; $result=mysql_query("SELECT names,courses FROM table WHERE names='".$select2."'");` where `$select2=$_POST['select2'];` could use some security to avoid sql injection – mplungjan May 14 '18 at 07:51

0 Answers0