title: How to chain dropdown boxes using PHP and MYSQL?
body: <p>This is my db:</p>
  
  <pre><code>          [tblset]              |     [tblyear]    |    [tbl_coursetype]
  ID, setname, setyear, setcours  |   ID, YearName   | course_no, course_desc   
  ------------------------------  | ---------------- | -----------------------
  1      A        1         1     |   1   1st Year   |     1          BSIT
  2      B        3         2     |   2   2nd Year   |     2          BSED
                                  |   3   3rd Year   |
                                  |   4   4th Year   |
                                  |   5   5th Year   |
  </code></pre>
  
  <ul>
  <li><p>And this is my code:
  </p>
  
  <pre><code>    &lt;form role="form" action="save_stud.php" method="post"&gt;
          &lt;div class="form-group"&gt;
          &lt;label&gt;First Name&lt;/label&gt;
          &lt;input type="text" name="studfname" class="form-control" required&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
          &lt;label&gt;Last Name&lt;/label&gt;
          &lt;input type="text" name="studlname" class="form-control" required&gt;
          &lt;/div&gt;
  
  
          &lt;div class="form-group"&gt;
          &lt;label&gt;Student Course&lt;/label&gt; 
          &lt;select name="studcourse" class="form-control"&gt;
                    &lt;?php
  
                          // Five steps to PHP database connections:
  
                          // 1. Create a database connection
                          //      (Use your own servername, username and password if they are different.)
                          //      $connection allows us to keep refering to this connection after it is established
                          $connection = mysql_connect("localhost","root",""); 
                          if (!$connection) {
                              die("Database connection failed: " . mysql_error());
                          }
  
                          // 2. Select a database to use 
                          $db_select = mysql_select_db("studsystem",$connection);
                          if (!$db_select) {
                              die("Database selection failed: " . mysql_error());
                          }
  
                      ?&gt;
  
              &lt;?php
                  $result = mysql_query("Select setcours, course_desc 
                      from tblset, tbl_coursetype 
                      where tbl_coursetype.course_no=tblset.setcours 
                      group by setcours", $connection);
  
                      if (!$result) {
                      die("Database query failed: " . mysql_error());
                      }
  
  
                    // 4. Use returned data
                          while ($row = mysql_fetch_array($result)) {  
                              echo "&lt;option value=\"{$row[0]}\"&gt;{$row[1]}&lt;/option&gt;";  
  
                          }
  
              ?&gt;
  
          &lt;/select&gt; 
          &lt;/div&gt;
  
  
          &lt;div class="form-group"&gt;
          &lt;label&gt;Student Year&lt;/label&gt; 
          &lt;select name="studyear" class="form-control"&gt;
  
                    &lt;?php
                          // Five steps to PHP database connections:
  
                          // 1. Create a database connection
                          //      (Use your own servername, username and password if they are different.)
                          //      $connection allows us to keep refering to this connection after it is established
                          $connection = mysql_connect("localhost","root",""); 
                          if (!$connection) {
                              die("Database connection failed: " . mysql_error());
                          }
  
                          // 2. Select a database to use 
                          $db_select = mysql_select_db("studsystem",$connection);
                          if (!$db_select) {
                              die("Database selection failed: " . mysql_error());
                          }
  
                      ?&gt;
  
                  &lt;?php
                  $result = mysql_query("Select setyear, YearName 
                      from tblset, tblyear
                      where tblyear.ID=tblset.setyear 
                      group by setyear", $connection);
  
                      if (!$result) {
                      die("Database query failed: " . mysql_error());
                  }
                    // 4. Use returned data
                          while ($row = mysql_fetch_array($result)) {  
                              echo "&lt;option value=\"{$row[0]}\"&gt;{$row[1]}&lt;/option&gt;";  
  
                          }
  
              ?&gt;
  
  
          &lt;/select&gt; 
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
          &lt;label&gt;Section&lt;/label&gt; 
          &lt;select name="studset" class="form-control"&gt;
  
                    &lt;?php
                          // Five steps to PHP database connections:
  
                          // 1. Create a database connection
                          //      (Use your own servername, username and password if they are different.)
                          //      $connection allows us to keep refering to this connection after it is established
                          $connection = mysql_connect("localhost","root",""); 
                          if (!$connection) {
                              die("Database connection failed: " . mysql_error());
                          }
  
                          // 2. Select a database to use 
                          $db_select = mysql_select_db("studsystem",$connection);
                          if (!$db_select) {
                              die("Database selection failed: " . mysql_error());
                          }
  
                      ?&gt;
  
              &lt;?php
                  $result = mysql_query("SELECT * FROM tblset t LIMIT 0,1000", $connection);
                      if (!$result) {
                      die("Database query failed: " . mysql_error());
                  }
  
                    // 4. Use returned data
                          while ($row = mysql_fetch_array($result)) {  
                              echo "&lt;option value=\"{$row[0]}\"&gt;{$row[1]}&lt;/option&gt;";  
  
                          }
  
              ?&gt;
  
  
          &lt;/select&gt; 
          &lt;/div&gt;
  </code></pre></li>
  </ul>
  
  <p>-Here are some screenshots. 
  <a href="https://i.stack.imgur.com/UrUXB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UrUXB.png" alt="Choosing course"></a>
  <a href="https://i.stack.imgur.com/XJmyw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XJmyw.png" alt="Choosing year level"></a></p>
  
  <p>Now, I am confused because <code>BSIT</code> has only <code>1st year</code> but when everytime I choose <code>BSIT</code>, the second dropdown will also appear <code>3rd year</code>, which infact, <code>3rd year</code> is only intended for <code>BSED</code>. </p>
  
  <p>In short, I want that when everytime I choose a course, it will automatically change the second dropdown menu with its corresponding year level/s.</p>
  
  <p>I am new at php and mysql, and I don't know how to fix this issue. I've been trying to search any code over the internet for almost 1 week but no one's working, maybe I don't know how so I can't trace why it does so.</p>
  
  <p>Can someone help me? Any help will be appreciated. Thanks!</p>