0

I have a form in my html page to insert data into the database. In the form I have drop down lists for some fields. I would like to get the data for the drop down list from the database.

Following is the code used before ( hard coded drop down )

<div class="form-group">
<label for="proj_name" class="col-sm-4 control-label">Project Name</label>
<div class="col-sm-8">
    <select name="proj_name"  id="proj_name" class="form-control" >
        <option selected="true" value="" data-hidden="true" style="color:red">Choose Project</option>
        <option value="Project1">Project1</option>
        <option value="Project2">Project2</option>
        <option value="Project3">Project3</option>
        <option value="Project4">Project4</option>
    </select>   
</div>

I am using PDO to connect to the database. ( Also using bootstrap template for the form ). Now I want to populate those project names using the data in project table.

I am using following code modifying the above mentioned ones:

<div class="form-group">
    <label for="proj_name" class="col-sm-3 control-label" style="color:red">Project Name</label>
    <div class="col-sm-9">
        <?php

            try
            {
                 $sql = "select proj_id,proj_name from tblProject";
                 $projresult = $db->query($sql);                       
                 $projresult->setFetchMode(PDO::FETCH_ASSOC);


            }
            catch (PDOException $e)
            {   
                die("Some problem getting data from database !!!" . $e->getMessage());
            }

            echo '<select name="proj_name"  id="proj_name" class="form-control" >';

            while ( $row = $projresult->fetch() ) 
            {
               echo '<option value="'.$row['proj_name'].'">'.$row['proj_name'].'</option>';
            }

            echo '</select>';

        ?>                                                          

    </div>
</div>

The form is in a bootstrap modal and when I use this code, I am unable to open the modal. Also not sure if the code is correct usage to get that data for the drop down lists.

Please correct me if am wrong anywhere. Am new to using PDO.

Any suggestions are welcomed

4
  • method used? form? POST arrays? unclear Commented Oct 26, 2015 at 15:39
  • Hi, method used for form submission is post. Any other information required ?? Commented Oct 26, 2015 at 15:45
  • have you tried to run this code in a page and not in a modal? what do you get in this case? Commented Oct 26, 2015 at 16:47
  • This code is running when it is run seperately in a page. But run in a bootstrap modal, the modal is not opening at all, so dont know if it works. ( previously there was a mistake in try section, had corrected it ) Commented Oct 26, 2015 at 17:08

2 Answers 2

1

I don't see anything wrong with the code, per se, but I would suggest moving your FETCH call inside your try/catch.

If you FETCH call is failing, at this point, you aren't catching the exception anywhere, and you want to catch that exception before you try to dump the rest of the data to the screen.

EDIT: Example:

        try
        {
             $sql = "select proj_id,proj_name from tblProject";
             $projresult = $db->query($sql);                       
             $projresult->setFetchMode(PDO::FETCH_ASSOC);


             echo '<select name="proj_name"  id="proj_name" class="form-control" >';

             while ( $row = $projresult->fetch() ) 
             {
                echo '<option value="'.$row['proj_name'].'">'.$row['proj_name'].'</option>';
             }

             echo '</select>';
        }
        catch (PDOException $e)
        {   
            die("Some problem getting data from database !!!" . $e->getMessage());
        }

I am not 100% sure about this, but I believe the actual execute statement does not happen until you make the actual ->fetch() call, which would mean that if your SQL was bad or your server connection was bad, no exception would be thrown until this point, and your original try/catch block would miss it.

Sign up to request clarification or add additional context in comments.

3 Comments

@sravankumar I added a code block and some more explanation.
Thank you. I shall look into it
However it dint completely solve my problem but the one you suggested is a better programming practice
1

This issue has been solved.

Problem was in the modal it was not connected to the database. Somehow this is preventing the modal from opening.

Following is the correct code

<div class="form-group">
    <label for="proj_id" class="col-sm-3 control-label" style="color:red">Project ID *</label>
    <div class="col-sm-9">

    <?php 

        require 'DBconnect.php';    // connect to Database

        try
        {
                 $sql = "select proj_id,proj_name from tblProject";
                 $projresult = $db->query($sql);
                 $projresult->setFetchMode(PDO::FETCH_ASSOC);

                 echo '<select name="proj_id"  id="proj_id" class="form-control" >';

             while ( $row = $projresult->fetch() ) 
             {
                echo '<option value="'.$row['proj_id'].'">'.$row['proj_name'].'</option>';
             }

             echo '</select>';
            }
            catch (PDOException $e)
            {   
                die("Some problem getting data from database !!!" . $e->getMessage());
            }



    ?>

   </div>
</div>

Taking into consideration Mark's suggestion.

DBconnect.php has the details to connect to my database.

1 Comment

Do you understand why, though? The Database Connection is not opened and and the SQL is not executed until the ->fetch() call, so if there is an error with your connection you must make sure you are catching exceptions AFTER the ->fetch() call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.