Taking data from database in populate drop down box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agarwalsrushti
    New Member
    • Feb 2008
    • 63

    #1

    Taking data from database in populate drop down box

    Hello,
    I ve created a php form for user registration in which have also used the drop down boxes.
    I want that data in the drop down box should be taken from mysql database.
    The data in qualification drop down should be taken from database and depending on what the user selected in qualification it should take the corresponding data for specialization drop down again from database itself. How can this be done. Im stuck up with it.
    Plz help me for this.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Show any code you have developed (within the appropriate code tags PLEASE) and we will look at it.

    Ronald

    Comment

    • agarwalsrushti
      New Member
      • Feb 2008
      • 63

      #3
      hi,
      thanks. Ive uploaded the txt file of the html code i developed. I want this to be in php. The Qualification, Specialization, Role, etc. are drop down boxes for which i need to take data from database. In this code i ve manually specified the values.
      In this there are only certain values inserted. I ll b adding more if i get it done thru database.
      Plz check this out.
      Attached Files
      Last edited by agarwalsrushti; Mar 4 '08, 06:07 PM. Reason: not shown properly

      Comment

      • muppetjones
        New Member
        • Feb 2008
        • 11

        #4
        Originally posted by agarwalsrushti
        hi,
        thanks. Ive uploaded the txt file of the html code i developed. I want this to be in php. The Qualification, Specialization, Role, etc. are drop down boxes for which i need to take data from database. In this code i ve manually specified the values.
        In this there are only certain values inserted. I ll b adding more if i get it done thru database.
        Plz check this out.
        I assume the options will be static once populated from the database? If so, you should be able to use php (make sure your server/host is configured to allow php--most are now days) to query the database and populate the list. Check out www.php.net for more.

        Here's an example:
        [code=php]
        $query = "SELECT options FROM qualifications" ;
        $result = mysql_query($qu ery,$link);
        if(!$result) {
        die(mysql_error ());
        }

        while($row = mysql_fetch_row ($result)) {
        $list[] = $row[0];
        }
        [/code]
        Where $link is a valid mysql database connection and $list is the array of your options.

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          We are talking about these 4 pieces of code, I presume:[code=html] <select name="AreaName" id="AreaName">
          <OPTION value="" selected>-Select-</OPTION>
          <OPTION>Corpora te Planning/Consulting</OPTION>
          <OPTION>Enginee ring Design/R and D</OPTION>
          <OPTION>HR/Administration/IR</OPTION>
          <OPTION>IT Software-Client Server</OPTION>
          <OPTION>IT Software-Mainframe</OPTION>
          <OPTION>IT Software-Middleware</OPTION>
          <OPTION>IT Software-Mobile</OPTION>
          <OPTION>IT Software-Other</OPTION>
          <OPTION>IT Software-System Programming</OPTION>
          <OPTION>IT Software-Telecom Software</OPTION>
          <OPTION>IT Software-Application Programming/Maintenance</OPTION>
          <OPTION>IT Software-DBA/Datawarehousing </OPTION>
          <OPTION>IT Software-E-Commerce/Internet Technologies</OPTION>
          <OPTION>IT Software-Embedded/EDA/VLSI/ASIC/Chip Des.</OPTION>
          <OPTION>IT Software- ERP/CRM</OPTION>
          <OPTION>IT Software-Network Administration/Security</OPTION>
          <OPTION>IT Software-QA and Testing</OPTION>
          <OPTION>IT Software-Systems/EDP/MIS</OPTION>
          <OPTION>IT-Hardware/Telecom/Technical Staff/Support</OPTION>
          <OPTION>ITES/BPO/KPO/Customer Service/Operations</OPTION>
          <OPTION>Site Engineering/Project Management</OPTION>
          <OPTION>Web/Graphic Design/Visualiser</OPTION>
          <OPTION>Other </OPTION>
          </select>[/code][code=html] <select name="Qualifica tionName" size="1" id="Qualificati onName">
          <OPTION value="" selected>- Select -</OPTION>
          <OPTION>Aviatio n</OPTION>
          <OPTION>B.A</OPTION>
          <OPTION>B.Arc h</OPTION>
          <OPTION>B.B.A </OPTION>
          <OPTION>BCA</OPTION>
          <OPTION>B.Com </OPTION>
          <OPTION>BDS</OPTION>
          <OPTION>B.E/B.Tech</OPTION>
          <OPTION>B.Ed</OPTION>
          <OPTION>BHM</OPTION>
          <OPTION>BL/LLB</OPTION>
          <OPTION>B.Pharm </OPTION>
          <OPTION>B.Sc</OPTION>
          <OPTION>CA</OPTION>
          <OPTION>Class 12</OPTION>
          <OPTION>CS</OPTION>
          <OPTION>Diploma </OPTION>
          <OPTION>ICWA</OPTION>
          <OPTION>M.A</OPTION>
          <OPTION>M.Arc h</OPTION>
          <OPTION>MBA</OPTION>
          <OPTION>MBBS</OPTION>
          <OPTION>MCA</OPTION>
          <OPTION>M.Com </OPTION>
          <OPTION>MD/MS</OPTION>
          <OPTION>M.Ed</OPTION>
          <OPTION>M.E/M.Tech/MS</OPTION>
          <OPTION>ML/LLM</OPTION>
          <OPTION>M.Pharm </OPTION>
          <OPTION>Mphil </OPTION>
          <OPTION>M.Sc</OPTION>
          <OPTION>PGDCA </OPTION>
          <OPTION>PG Diploma</OPTION>
          <OPTION>PGDM</OPTION>
          <OPTION>Phd</OPTION>
          <OPTION>Other </OPTION>
          </select>[/code][code=html] <select name="Specializ ationArea" size="1" id="Specializat ionArea">
          <OPTION value="" selected>- Select -</OPTION>
          <OPTION>Automob ile</OPTION>
          <OPTION>Bio-Technology</OPTION>
          <OPTION>Bio-Medical</OPTION>
          <OPTION>Chemica l</OPTION>
          <OPTION>Civil </OPTION>
          <OPTION>Compute rs</OPTION>
          <OPTION>Electic al</OPTION>
          <OPTION>Electon ics/Telecom</OPTION>
          <OPTION>Instrum entation</OPTION>
          <OPTION>Enginee ring Physics</OPTION>
          <OPTION>Mechani cal</OPTION>
          <OPTION>Metallu rgy</OPTION>
          <OPTION>Mineral </OPTION>
          <OPTION>Minin g</OPTION>
          <OPTION>Other Engineering</OPTION>
          </select>[/code][code=html]
          <select name="RoleName" size="1" id="RoleName">
          <OPTION value="" selected>- Select -</OPTION>
          <OPTION>Fresher </OPTION>
          <OPTION>Trainee </OPTION>
          <OPTION>Softwar e Engineer/Programmer</OPTION>
          <OPTION>Team Leader/Technical Leader</OPTION>
          <OPTION>Syste m Analyst/Tech Architect</OPTION>
          <OPTION>Databas e Architect/Designer</OPTION>
          <OPTION>Proje ct Leader/Project Manager</OPTION>
          <OPTION>Deliver y Manager</OPTION>
          <OPTION>Progr am Manager</OPTION>
          <OPTION>Databas e Administrator DBA)</OPTION>
          <OPTION>Syste m Administrator</OPTION>
          <OPTION>Netwo rk Administrator</OPTION>
          <OPTION>Netwo rk Designer</OPTION>
          <OPTION>Syste m Security-Engineer</OPTION>
          <OPTION>Securit y Analyst</OPTION>
          <OPTION>Softwar e Test Engineer</OPTION>
          <OPTION>Syste m Integrator</OPTION>
          <OPTION>Technic al Support Engineer</OPTION>
          <OPTION>S/W Installation/Maintenance Engg</OPTION>
          <OPTION>Produ ct Manager</OPTION>
          <OPTION>Busines s Analyst</OPTION>
          <OPTION>Datawar ehousing Consultants</OPTION>
          <OPTION>ERP,C RM-Technical Consultant</OPTION>
          <OPTION>ERP, CRM - Functional Consultant</OPTION>
          <OPTION>ERP, CRM - Support Engineer</OPTION>
          <OPTION>Graph ic Designer/Animator</OPTION>
          <OPTION>Configu ration Mgr/Release Manager</OPTION>
          <OPTION>Web Master/Web Site Manager</OPTION>
          <OPTION>Hardwar e Design Engineer</OPTION>
          <OPTION>Hardwar e Design Technical Leader</OPTION>
          <OPTION>H/W Installation/Maintenance Engg</OPTION>
          <OPTION>Quali ty Assurance Executive</OPTION>
          <OPTION>Quali ty Assurance-Manager</OPTION>
          <OPTION>VP/ GM-Quality</OPTION>
          <OPTION>Compute r Operator/Data Entry</OPTION>
          <OPTION>Technic al Writer</OPTION>
          <OPTION>Instruc tional Designer</OPTION>
          <OPTION>Train er/Faculty</OPTION>
          <OPTION>EDP Analyst</OPTION>
          <OPTION>IT/ Networking (EDP)-Manager</OPTION>
          <OPTION>Informa tion Systems (MIS)-Manager</OPTION>
          <OPTION>Chief Technology Officer</OPTION>
          <OPTION>Chief Information Officer</OPTION>
          <OPTION>VP/ Head-Technology (IT)</OPTION>
          <OPTION>VP-Operations/COO</OPTION>
          <OPTION>SBU Head/Profit Centre Head</OPTION>
          <OPTION>CEO/MD/Country Manager</OPTION>
          <OPTION>Directo r on Board</OPTION>
          <OPTION>Externa l Consultant</OPTION>
          <OPTION>Other Software/Hardware/EDP</OPTION>
          </select>[/code]I suggest that we concentrate on the first select drop down set. When you know how to populate that one, you can do the others in a similar way.

          Ronald

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            The next code can (when adapted) also be used for the other 3 select lists:[php]<?php

            // Make connection to server and database
            $conn = mysql_connect($ dbhost, $dbuser, $dbpass)
            or die('Error connecting to mysql');
            mysql_select_db ('DB_name');

            // select the data from db
            $result = mysql_query( "SELECT * FROM table_name" )
            or die("SELECT Error: ".mysql_error() );

            // start the select list
            echo '<select name="AreaName" id="AreaName">' ;
            echo '<OPTION value="" selected>-Select-</OPTION>';

            // read each row field and build option list
            while ($row = mysql_fetch_ass oc($result)) {
            $field_name=$ro w['field_name'];
            print "<option value='$field_n ame'>$field_nam e</option>";
            }

            // close the select list
            print "</select>";[/php]Ronald

            Comment

            • agarwalsrushti
              New Member
              • Feb 2008
              • 63

              #7
              hi,
              I saw ur code. Bt im not able to egt the data from database. Im not geting where to place it exactly in the php code. The table name from which ive to take info is admin. The fields in it are QualificationId and QualificationNa me in which the data to be viewed by the user is there. Ive attached my php file plz help me in teling how exactly it wil be placed.
              Thanks a lot
              Attached Files
              Last edited by agarwalsrushti; Mar 5 '08, 08:20 AM. Reason: file not attached properly

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                When you say "I am not able to get the data from the database"when what is the point of this exercise?

                If you don't have the data from the db, then you cannot fill the drop down list.

                So what is then the problem? Please describe it in understandable english, so I do not have toi guess what you mean.

                Ronald

                Comment

                • agarwalsrushti
                  New Member
                  • Feb 2008
                  • 63

                  #9
                  I have filled the data in database. I tried the code u gave me. Using that i was not able to get the data in drop down box. I wanted to know exactly where in my code it ll b placed so that it should retrieve the data.
                  Thanks.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    It is not that difficult, just replace the build select list with name 'Areaname' in your code with the PHP code I showed in my previous post.

                    So replace this code [code=html] <select name="AreaName" id="AreaName">
                    <OPTION value="" selected>-Select-</OPTION>
                    <OPTION>Corpora te Planning/Consulting</OPTION>
                    <OPTION>Enginee ring Design/R and D</OPTION>
                    <OPTION>HR/Administration/IR</OPTION>
                    <OPTION>IT Software-Client Server</OPTION>
                    <OPTION>IT Software-Mainframe</OPTION>
                    <OPTION>IT Software-Middleware</OPTION>
                    <OPTION>IT Software-Mobile</OPTION>
                    <OPTION>IT Software-Other</OPTION>
                    <OPTION>IT Software-System Programming</OPTION>
                    <OPTION>IT Software-Telecom Software</OPTION>
                    <OPTION>IT Software-Application Programming/Maintenance</OPTION>
                    <OPTION>IT Software-DBA/Datawarehousing </OPTION>
                    <OPTION>IT Software-E-Commerce/Internet Technologies</OPTION>
                    <OPTION>IT Software-Embedded/EDA/VLSI/ASIC/Chip Des.</OPTION>
                    <OPTION>IT Software- ERP/CRM</OPTION>
                    <OPTION>IT Software-Network Administration/Security</OPTION>
                    <OPTION>IT Software-QA and Testing</OPTION>
                    <OPTION>IT Software-Systems/EDP/MIS</OPTION>
                    <OPTION>IT-Hardware/Telecom/Technical Staff/Support</OPTION>
                    <OPTION>ITES/BPO/KPO/Customer Service/Operations</OPTION>
                    <OPTION>Site Engineering/Project Management</OPTION>
                    <OPTION>Web/Graphic Design/Visualiser</OPTION>
                    <OPTION>Other </OPTION>
                    </select>[/code]
                    with this code[php]<?php

                    // Make connection to server and database
                    $conn = mysql_connect($ dbhost, $dbuser, $dbpass)
                    or die('Error connecting to mysql');
                    mysql_select_db ('DB_name');

                    // select the data from db
                    $result = mysql_query( "SELECT * FROM table_name" )
                    or die("SELECT Error: ".mysql_error() );

                    // start the select list
                    echo '<select name="AreaName" id="AreaName">' ;
                    echo '<OPTION value="" selected>-Select-</OPTION>';

                    // read each row field and build option list
                    while ($row = mysql_fetch_ass oc($result)) {
                    $field_name=$ro w['field_name'];
                    print "<option value='$field_n ame'>$field_nam e</option>";
                    }

                    // close the select list
                    print "</select>";[/php]When you have done that and understand the php code, you then know how to dynamically build a drop down list in PHP, and you can do the other drop down lists populates yourself.

                    Ronald

                    Comment

                    • agarwalsrushti
                      New Member
                      • Feb 2008
                      • 63

                      #11
                      Hi,
                      I ve placed that code as you told in my php file and tried to execute it. The drop down box is seen but the values are not displayed in it. Only select is displayed and when i click on it it shows everything blank. I ve the data in database. Ive pasted the code which i used according to the table names. Plz check this.

                      [code=php]
                      <?php

                      // Make connection to server and database
                      $dbi=mysql_conn ect($hostname, $db_user, $db_password)
                      or die('Error connecting to mysql');
                      mysql_select_db ($db_name,$dbi) ;

                      // select the data from db
                      $result = mysql_query( "SELECT * FROM admin" )
                      or die("SELECT Error: ".mysql_error() );

                      // start the select list
                      echo '<select name="Qualifica tionName">';
                      echo '<OPTION value="" selected>-Select-</OPTION>';

                      // read each row field and build option list
                      while ($row = mysql_fetch_ass oc($result)) {
                      $field_name = $row['field_name'];
                      print "<option value='$field_n ame'>$field_nam e</option>";
                      }

                      // close the select list
                      print "</select>";
                      ?>
                      [/code]

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        I can cook it but I will not eat it!!

                        You have to do something yourself on the code. How can you copy this field field_name literally? It is an example of course. How would I knmow what the name of your db field is?

                        So YOU have to change those statement with $field_name = $row['field_name']; into the name that is in your database!!

                        Ronald

                        Comment

                        • agarwalsrushti
                          New Member
                          • Feb 2008
                          • 63

                          #13
                          Hi,
                          That problem has got solved and it is working now.
                          Now i wanted dt when a user selects a qualification name then the coresponding specializations should be displayed in the drop down menu. So do i ve to use a query where in it compares the qualification id coz in specialization table i ve a field called qualificationid .
                          How can this be done. Thanks a lot.

                          Comment

                          • ronverdonk
                            Recognized Expert Specialist
                            • Jul 2006
                            • 4259

                            #14
                            This goes exactly in the same way as the one you just implemented. You only must change your select statement, the rowid and the name of the select list.

                            Ditto for the other ones. Just take the code I showed you and adapt it to the other drop down select lists.

                            Ronald

                            Comment

                            • agarwalsrushti
                              New Member
                              • Feb 2008
                              • 63

                              #15
                              Ive used the same code for others and it is working but in case of specialization my need is different. How can i get the qualificationId for the qualification name that the user selected. Each qualification has diferent specialization list. Depending on the qualification selected specializaton list should be displayed. The qualification Id is stored in the same table where the drop down list of names is stored. There is a id for each name. Im not able to retrieve the qualification id of the qualification selected to modify my select statement. Plz let me know.

                              Comment

                              Working...