Code
Breakdown
1. Database Connection
require
'db_connection.php';
- This
line includes the
db_connection.php
file, which establishes the database connection using the$conn
variable. - Without
this file, the script won't be able to query the database.
2. Fetching the Course ID
$course_id =
$_GET[
'course_id'];
- Retrieves
the
course_id
from the URL using the$_GET
superglobal. - The
course_id
is used to filter semesters based on the selected course.
Example:
If the URL is script.php?course_id=5
,
$course_id
will contain the value 5
.
3. SQL Query
$query =
"SELECT id, name FROM semesters WHERE course_id = $course_id";
- A
SELECT
query retrieves theid
andname
columns from thesemesters
table, filtering rows wherecourse_id
matches the provided value.
Example
Query for course_id = 5
:
SELECT id, name
FROM semesters
WHERE course_id
=
5;
4. Executing the Query
$result =
$conn->
query(
$query);
- The
query is executed using the
$conn->query()
method. - The
result is stored in the
$result
variable.
5. Checking Results
if (
$result->num_rows >
0) {
- Checks
if the query returned any rows.
num_rows
: Indicates the number of rows in the result set.
6. Generating Dropdown Options
·
Default Option:
echo
'<option value="">Select Semester</option>';
- Adds
a default option prompting the user to select a semester.
· Iterating Through Rows:
while (
$row =
$result->
fetch_assoc()) {
echo
"<option value='{$row['id']}'>{$row['name']}</option>";
}
fetch_assoc()
fetches each row as an associative array.- For
each row:
- The
id
is set as thevalue
attribute of the<option>
tag. - The
name
is displayed as the visible text in the dropdown.
Example Output for Semesters:
<option value="">Select Semester
</option>
<option value="1">Semester 1
</option>
<option value="2">Semester 2
</option>
<option value="3">Semester 3
</option>
7. Handling No Results
{
echo
'<option value="">No Semesters Available</option>';
}
- If
the query returns zero rows, an
<option>
is added to inform the user that no semesters are available.
Expected
Usage
This
script is often used to populate a semester dropdown in a cascading dropdown
system.
Example Workflow:
1.A
user selects a course in a dropdown menu.
2.Using
JavaScript (e.g., AJAX
), the course_id
is sent to this
script via a GET
request.
3.The
script generates a list of semesters for the selected course, which is
dynamically displayed in the semester dropdown.
Example
Dropdown:
<select name="semester" id="semester-dropdown">
<?php include 'generate_semesters.php?course_id=5'; ?>
</select>
Key
Features
1.Dynamic Data Loading:
o Dropdown
options are dynamically generated based on the selected course.
2.Database Integration:
o Fetches
semesters from the database, ensuring up-to-date data.
3.Empty State Handling:
o Displays
an appropriate message if no semesters are available.
Potential
Improvements
1.Validation and Error Handling:
o Validate
course_id
to ensure
it’s a valid integer.
if (!
is_numeric(
$course_id)) {
die(
'Invalid Course ID');
}
o Check
if the query execution was successful.
if (!
$result) {
die(
"Query failed: " .
$conn->error);
}
2.SQL Injection Prevention:
o Use
prepared statements to secure the query.
Example:
$stmt =
$conn->
prepare(
"SELECT id, name FROM semesters WHERE course_id = ?");
$stmt->
bind_param(
"i",
$course_id);
// 'i' indicates integer
$stmt->
execute();
$result =
$stmt->
get_result();
3.Output Sanitization:
o Escape
special characters to prevent XSS attacks.
echo
"<option value='" .
htmlspecialchars(
$row[
'id']) .
"'>" .
htmlspecialchars(
$row[
'name']) .
"</option>";
How
It Works
When
included in a form, the script dynamically generates <option>
elements
for semesters based on the course selected by the user.
Example
Scenario:
- A
user selects "Computer Science" (course ID: 5).
- The
script runs with
?course_id=5
. - The
dropdown is populated with semesters available for "Computer
Science."
This approach ensures the dropdown options are always in sync with the database.