Drop Down list from database in PHP/ NOT WORKING ........HELP

Discussion in 'PHP' started by prajita, Oct 27, 2007.

  1. #1
    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
     
    prajita, Oct 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Oct 27, 2007 IP
  3. prajita

    prajita Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope its not working.....
     
    prajita, Oct 27, 2007 IP
  4. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    Dirty-Rockstar, Oct 27, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    I replied to your other thread. Did you try what I said?


    And please be more specific. What exactly "does not work"?
     
    nico_swd, Oct 27, 2007 IP
  6. prajita

    prajita Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    prajita, Oct 27, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    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());
    
    ?>
    
    PHP:
     
    nico_swd, Oct 27, 2007 IP
  8. prajita

    prajita Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I really appreciate....

    The code works

    What did you do?

    You are a magician....

    Thanks
     
    prajita, Oct 27, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Actually, there's a slight typo:
    
    print '<select name=\"studentid"\>';
    
    PHP:
    Should be:
    
    print '<select name="studentid">';
    
    PHP:
    (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.
     
    nico_swd, Oct 27, 2007 IP
  10. prajita

    prajita Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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 27, 2007 IP
  11. prajita

    prajita Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thank you anyways..........

    I figured out the mistake i made.

    Thanks
     
    prajita, Oct 27, 2007 IP