By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,854 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,854 IT Pros & Developers. It's quick & easy.

How to display information from database using value selected in listbox

P: 4
I want to display information when value in listbox selected.

Below is my code:-

This code to get value option in listbox
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $query = "select courseTitle from training where startDate > NOW() ";
  3. $result = mysql_query($query);
  4. print "<SELECT name=item>";
  5. while ($line = mysql_fetch_array($result))
  6. {
  7. foreach ($line as $value)
  8. {
  9. print "<OPTION value='$value'";
  10. }
  11. print ">$value</OPTION>";
  12. }
  13.  
  14. mysql_close($link);
  15. print "</SELECT>";    
  16. ?>
This code to display information from database when select value in listbox
Expand|Select|Wrap|Line Numbers
  1. <?
  2. if (isset($_GET['item']))
  3. {
  4.  
  5. $query=mysql_query ("SELECT training_id from training where courseTitle='$item' "); 
  6.  
  7.  
  8. while( $row = mysql_fetch_array($query) {
  9.  
  10.                $training_id=$row['training_id'];
  11.  
  12. echo $row['training_id'];
  13.  
  14. }
  15. }
  16.  
  17. ?>
Anyone can solve my problem? Thanks.
Apr 12 '10 #1
Share this Question
Share on Google+
7 Replies


Atli
Expert 5K+
P: 5,058
Hey.

The $item variable you use in your query has not been defined. You need to fetch the variable from the $_GET array before you can use it. (In most cases, anyways.)
Apr 12 '10 #2

P: 4
@Atli
can u give me an example how to use $_GET array for my coding so that i can implement in my code above..really appreciate!
Apr 12 '10 #3

Atli
Expert 5K+
P: 5,058
You fetch an array element into a variable like so:
Expand|Select|Wrap|Line Numbers
  1. $variable = $array['element'];
The $_GET array works just like a normal array.

See the manual for more details:
- PHP: $_GET - Manual
- PHP: Arrays - Manual
Apr 12 '10 #4

P: 4
@Atli
Thanks atli..i will try implement it.
Apr 12 '10 #5

NettSIte
P: 7
Expand|Select|Wrap|Line Numbers
  1. $query=mysql_query ("SELECT training_id from training where courseTitle='{$_GET['item']}' ");  
Should do the trick.
Apr 13 '10 #6

Atli
Expert 5K+
P: 5,058
@NettSIte
You should never put $_GET or$_POST variables directly into a query. Not unless you want your database to be hacked.

Imagine if the value of $_GET['item'] were:
Expand|Select|Wrap|Line Numbers
  1. ' OR 1='1
Or, even worse:
Expand|Select|Wrap|Line Numbers
  1. '; DROP TABLE training;--
You should always run your variables through mysql_real_escape_string before putting them into a query.

See SQL Inection for more details on why this is necessary.
Apr 14 '10 #7

NettSIte
P: 7
@Atli
You should always run your variables through mysql_real_escape_string before putting them into a query.

True.
Apr 14 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.