1

I have just started using PDO but I am slowly getting the hang of it. I want to know how to make a drop down menu or list box populate the data into fields on a page. I have started the code by looking up PDO guides etc, but I am having trouble finding a solution for this. I am also sorry for the untidy code but again I am new to the whole programming scene.

Thanks in advance. Here is my code so far: Here is the connection string:

<?php
    session_start();

    if(!isset($_SESSION["user_id"])){
        header("location:../Pages/login.html");
    }


    //databse connection Sting
    $connection = new PDO("sqlsrv:server=servername;Database=databasename", "username",                     "password"); 

    //insertion function
    $smt = $connection->prepare('select exam_id From exam');



?>

This also included my session cookie, but that works great. Here is the population of the drop down box I have so far.

 <select name="lst_exam" id="lst_exam">

       <?php

            $smt->execute();
            while ($row = $smt->fetch()){
                echo "<option>" . $row["exam_id"] . "</option>";
            }
            $connection = null;
        if(isset($_POST["lst_exam"]));  

        ?>
    </select>

The text boxes I am trying to populate are txt_exam_id, txt_location, txt_date_taken, txt_exam_taken, txt_grade_recieved

2 Answers 2

13

The answer is simple: do not populate dropdown menus through pdo code

That's totally different matters which should never be intrmixed in the code.

Separate your code into 2 parts:

  • PDO code
  • populating whatever menus from a conventional array code.

write and debug these parts separately.

$smt = $connection->prepare('select exam_id From exam');
$smt->execute();
$data = $smt->fetchAll();

now you have your exams stored in $data array.

<select name="lst_exam" id="lst_exam">
<?php foreach ($data as $row): ?>
    <option><?=$row["exam_id"]?></option>
<?php endforeach ?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

1
    //USING PDO   

 $ID=trim($_GET['id']);
    $result = $DB_con->prepare("select userID, firstName, lastName, gender, telNo, userEmail, userName, contactAddress, case when userStatus = 'Y' then 'TRUE' ELSE 'FALSE' end as userStatus, case when state = 1 then 'ACTIVE' else 'IN-ACTIVE' end as state, department.name as department from users JOIN department on department.id = users.department where userID=:get_header LIMIT 1");
    $result->execute(array(":get_header"=>$ID));
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
    $id=$row['userID'];
    ?>

    $sql_g = "Select id, name from gender";
    $gend = $DB_con->prepare($sql_g);
    //Execute the statement.
    $gend->execute();
    //Retrieve the rows using fetchAll.
    $gend_lists = $gend->fetchAll(PDO::FETCH_ASSOC);


     //HTML AND PHP
    <div class="col-sm-3">
          <div class="form-group">
              <label class="control-label">Gender</label>
                 <?php $value = $row["gender"]; ?>
                 <select name="gender_test" class="form-control">
                      <?php foreach($gend_lists as $id => $option) { ?>
                        <option value="<?php echo $id["id"] ?>"<?php
                      if($id["id"] == $value) {
                  echo " selected";
             } ?>><?php echo $option["name"] ?></option>
          <?php } ?>
       </select>
     </div><!-- form-group -->
</div><!-- col-sm-6 -->

1 Comment

Add some description, it will help more than just a piece of code

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.