This is my db:
[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 |
And this is my code:
<form role="form" action="save_stud.php" method="post"> <div class="form-group"> <label>First Name</label> <input type="text" name="studfname" class="form-control" required> </div> <div class="form-group"> <label>Last Name</label> <input type="text" name="studlname" class="form-control" required> </div> <div class="form-group"> <label>Student Course</label> <select name="studcourse" class="form-control"> <?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()); } ?> <?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 "<option value=\"{$row[0]}\">{$row[1]}</option>"; } ?> </select> </div> <div class="form-group"> <label>Student Year</label> <select name="studyear" class="form-control"> <?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()); } ?> <?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 "<option value=\"{$row[0]}\">{$row[1]}</option>"; } ?> </select> </div> <div class="form-group"> <label>Section</label> <select name="studset" class="form-control"> <?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()); } ?> <?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 "<option value=\"{$row[0]}\">{$row[1]}</option>"; } ?> </select> </div>
Now, I am confused because BSIT
has only 1st year
but when everytime I choose BSIT
, the second dropdown will also appear 3rd year
, which infact, 3rd year
is only intended for BSED
.
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.
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.
Can someone help me? Any help will be appreciated. Thanks!