-3

I have 3 tables in my database (standards, courses, students). I want to display Fname and gender from students table enrolled in selected 'standard' and 'course' from dropdown - but the "submit" button doesn't seem to work. The code is as shown below. I am sure it is connected to the database as the dropdowns for 'courses' and 'standards' work:

 <html>
  <head>
    <title>Courses</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
   </head>
    <body>
     <div id="container">
        <div id="wrapper">
           <h1> Students</h1>   
              <div id="data">
            <form action="index.php" method="POST">
         <select name="standards">
            <option>Standard</option>
           <?php
            include 'includes/dbconnect.php';
             $query1 = "SELECT * FROM standards";
            $result1 = mysql_query($query1);
          while($rows1 = mysql_fetch_array($result1)){
              $standardID = $rows1['id'];
              $rowsData1 = $rows1['standardName'];

          ?>
           <option value="<?php echo $standardID; ?>">
        <?php
          echo $rowsData1; ?></option>
       <?php
         }
    ?>
    </select>
    </div>
    <div id="data2">
    <select name="courses">
    <option>Courses</option>
       <?php
         $query2 = "SELECT * FROM courses";
         $result2 = mysql_query($query2);
         while($rows2 = mysql_fetch_array($result2)){
              $coursesID = $rows2['id'];
              $rowsData2 = $rows2['courseName'];
          ?>
          <option value="<?php echo $courseID;?>">
       <?php echo $rowsData2; ?></option> <?php }?>
     </select>
     </div>

     <div id="submit">
     <input type="submit" name="submit" id="submit" value="submit"/>

     <table border="1" id="table1">
       <tr>
           <th>Student Name</th>
           <th>Gender</th>
       </tr>  

       <?php
         if(isset($_POST['submit'])){
         $standardName = $_POST['standards'];
         $courseName = $_POST['courses'];
         $query3 = "SELECT students.Fname, students.gender
            FROM students
            WHERE students.standardID = '$standardName'
            AND students.courseID = '$courseName'";
        $result3 = mysql_query($query3);
         while($rows3 = mysql_fetch_array($result3)){
              //$dataID = $rows3['id'];
              $studentName = $rows3['FName'];
              $gender = $rows3['gender'];
       ?>    
       <tr>
          <td><?php echo $studentName; ?></td>
          <td><?php echo $gender; ?></td>
          <tr>
          <?php
          }
          }
          ?>
          </table>

          </div> 
         </div>
        </body>
       </html>
Ketchup
  • 5
  • 4
  • 1) You should label your question as "php" and "mysql" - 2) Have you tested these SQL requests with a tool such as PHPmyAdmin? - 3) you only need to call your dbconnect once per php script if you don't close the connection to the database in the meantime – raphv May 19 '16 at 06:01
  • Thanks raphv- yes I queried on phpmyadmin and it worked... – Ketchup May 19 '16 at 06:32
  • 4
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) that has been [removed](http://php.net/manual/en/mysql.php) from PHP. You should select a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 19 '16 at 07:16

2 Answers2

0

Inscribe the input elements within a form tag with post as method. Input elements with type submit are used for submission of forms.

0

You only do anything with the submitted form data if that data includes a submit field.

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

However you have:

<input type="submit" name="submit" id="submit"/>

Your submit button don't have a value, so nothing will be included in the form data for it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • thanks Quentin I added the value="submit" ...what else do you think is missing/wrong to make it work – Ketchup May 19 '16 at 07:29