View Full Version : Drop Down list from database in PHP/ NOT WORKING ........HELP
prajita
Oct 27th 2007, 4:34 pm
Please help me with the code below:
echo "<select name=\"studentid\">";
while($row = mysql_fetch_row($results_id));
{
echo "<option value=\"".$row['stu_id']."\">".$row['stu_id']."</option>";
}
echo "</select>";
print '</select>';
In above code i am trying to create a drop down list from a data in a table in a database
The name of the field in table is stu_id
Please help
I really need your help
nico_swd
Oct 27th 2007, 4:36 pm
Your question has been answered here?
http://forums.digitalpoint.com/showthread.php?t=530644
:confused:
prajita
Oct 27th 2007, 5:03 pm
Nope its not working.....
Dirty-Rockstar
Oct 27th 2007, 5:12 pm
debug. echo or print out the vars, and then write if statements
if (isset($row)){
print "worked";
}else{
print "KABLAMMO";
}
checking to see if all vars and queries are working is the first way to start. :D works wonders for me
nico_swd
Oct 27th 2007, 5:14 pm
Nope its not working.....
I replied to your other thread. Did you try what I said?
And please be more specific. What exactly "does not work"?
prajita
Oct 27th 2007, 5:17 pm
I cannot populate the stu_id from the database into my drop down list Okay here is the total code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Search Results
</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
span.c1 {color: blue; font-size: 120%}
span.c2 { color: red; font-style:italic }
-->
</style>
</head>
<body>
<?php
$host= "localhost";
$user = "---------";
$passwd = "---------";
$database = "------------";
$connect = @mysql_connect($host, $user, $passwd)
or die("connect error: " . mysql_error());
$table_name = 'student';
@mysql_select_db($database)
or die('select_db error: ' . mysql_error());
print "<span class=\"c1\">$table_name Data</span><br>";
$query = "SELECT stu_id FROM $table_name";
print "The query is <span class=\"c2\"> $query </span> <br>";
$results_id=@mysql_query($query, $connect)
or die('query error: ' . mysql_error());
print '<select name=\"studentid"\>';
while ($row = mysql_fetch_assoc($result,MYSQL_ASSOC)) {
print '<option name=\"stu_id\"> $row["stu_id"]</option>';
}
print '</select>';
@mysql_close($connect) or die('close error: ' . mysql_error());
?>
</body>
</html>
I tried so many times but i still cannot display values in my drop down list.
i would appreciate your help
nico_swd
Oct 27th 2007, 5:26 pm
I cannot populate the stu_id from the database into my drop down list
I figured as much. What I meant was, what does actually happen? Any PHP errors (if so, post them), are the options just in blank?
Anyway, try this. I couldn't test it but I don't see why it wouldn't work.
<?php
$host= "localhost";
$user = "---------";
$passwd = "---------";
$database = "------------";
$table_name = 'student';
$connect = @mysql_connect($host, $user, $passwd) or die("connect error: " . mysql_error());
@mysql_select_db($database) or die('select_db error: ' . mysql_error());
print "<span class=\"c1\">$table_name Data</span><br>";
$query = "SELECT stu_id FROM $table_name";
$results = @mysql_query($query, $connect) or die('query error: ' . mysql_error());
print '<select name=\"studentid"\>';
while ($row = mysql_fetch_assoc($result))
{
print "<option name=\"{$row['stu_id']}\">{$row['stu_id']}</option>\n";
}
print '</select>';
@mysql_close($connect) or die('close error: ' . mysql_error());
?>
prajita
Oct 27th 2007, 5:32 pm
I really appreciate....
The code works
What did you do?
You are a magician....
Thanks
nico_swd
Oct 27th 2007, 5:36 pm
Actually, there's a slight typo:
print '<select name=\"studentid"\>';
Should be:
print '<select name="studentid">';
(No need to escape the double quotes, in a single quoted string.)
And compare the 2 codes carefully to see the changes I made. :)
Oh, and one last suggestion: Don't be afraid of wasting lines. They're for free. :) Plus the code will be MUCH more readable and it's easier to spot errors this way.
prajita
Oct 27th 2007, 6:23 pm
I am sorry to bug you like this but i really need help. this is my school project and i am trying my best. I already finished more than 70% and i am stuck on this.
I need to do the similar thing (creating drop down list from values in a table in a database) in the same page from another table named "course" and the name of attribute is "course_num".
I copied the same exact code from above(which is working and changed the name of all the required variable but the new drop down is not working........
<head>
<style type="text/css">
<!--
span.c1 {color: blue; font-size: 120%}
span.c3{ font-size:175%}
span.c4(text-align=center; font-size:175%}
span.c2 { color: red; font-style:italic }
-->
</style>
</head>
<body>
<?php
$host= "localhost";
$user = "rkarki";
$passwd = "Muna1!";
$database = "rkarki";
$connect = @mysql_connect($host, $user, $passwd)
or die("connect error: " . mysql_error());
$table_name = 'student';
@mysql_select_db($database)
or die('select_db error: ' . mysql_error());
print "<span class=\"c1\">$table_name Data</span><br>";
$query = "SELECT stu_id FROM $table_name";
print "The query is <span class=\"c2\"> $query </span> <br>";
$results_id=@mysql_query($query, $connect)
or die('query error: ' . mysql_error());
print "<span class=\"c3\"> Student ID</span> <br>";
print '<select name="studentid">';
while ($row = mysql_fetch_assoc($results_id))
{
print "<option name=\"{$row['stu_id']}\">{$row['stu_id']}</option>\n";
}
print '</select>';
@mysql_close($connect) or die('close error: ' . mysql_error());
print "<span class=\"c4\"> Course Number</span> <br>";
$query = "SELECT cours_num FROM course";
$results_id=@mysql_query($query, $connect)
or die('query error: ' . mysql_error());
print '<select name="CourseID">';
while ($row = mysql_fetch_assoc($results_id1))
{
print "<option name=\"{$row['cours_num']}\">{$row['cours_num']}</option>\n";
}
print '</select>';
print '</span>';
@mysql_close($connect) or die('close error: ' . mysql_error());
?>
</body>
</html>
prajita
Oct 27th 2007, 6:26 pm
thank you anyways..........
I figured out the mistake i made.
Thanks
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.