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 3 years ago

3.61K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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 "
method=\"GET\">
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 "
type=\"submit\" value=\"SELECT\" />

"
; $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

0 views
0 shares

profilepic.png
manpreet 3 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.

Similar Forum