Is it possible to Query a Mysql database from a field selected from dropdown menu populated from a Query in php

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


Hello i am new to php and i have tried to find a piece of code that i can use to complete the task i need, i currently have a page with a form set out to view the criteria of a course. also i have a dropdown menu which currently holds all the course codes for the modules i have stored in a database. my problem is when i select a course code i wish to populate the fields in my form to show all the information about the course selected. The code i am trying to get to work is as follows:

php 
session_start();
?>
 include ("dbcon.php") ?>
php

if(!isset($_GET['coursecode'])){ 
$Var ='%'; 
} 
else 
{ 
if($_GET['coursecode'] == "ALL"){ 
$Var = '%'; 
} else { 
$Var = $_GET['coursecode']; 
} 
}


echo "
Coursecode
"; $res=mysql_query("SELECT * FROM module GROUP BY mId"); if(mysql_num_rows($res)==0){ echo "there is no data in table.."; } else { echo ""; } echo "

"
; $query = "SELECT * FROM module WHERE coursecode LIKE '$Var' "; $result = mysql_query($query) or die("Error: " . mysql_error()); if(mysql_num_rows($result) == 0){ echo("No modules match your currently selected coursecode. Please try another coursecode!"); } ELSE { Coursecode: echo $row['coursecode']; Module: echo $row['mName']; echo $row['mCredits']; echo $row['TotalContactHours']; echo $row['mdescription']; echo $row['Syllabus']; } ?>

however i can only seem to get the last entry from my database any help to fix this problem or a better way of coding this so it works would be grateful

Thanks

profilepic.png
manpreet 2 years ago

The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.

Some tips:

1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:

$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   ...
}

2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.