populating dropdown menu through pdo code

独自空忆成欢 提交于 2020-03-14 05:24:45

问题


ok so i have only just stared using pdo but am slowlys getting the hang of it and i am wanting to know how to make a drop down menu or list pox populate the data into the fields on the page i have started the code by looking up pdo guides etc but im having trouble finiding a solution for this. also sorry for the untidy code but again i am new to the whole programming scene

thanks in advice here is my code for it 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');



?>

that also includes my session cookie but that works great and 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 poulate are txt_exam_id, txt_location, txt_date_taken, txt_exam_taken, txt_grade_recieved


回答1:


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>



回答2:


    //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 -->


来源:https://stackoverflow.com/questions/16812733/populating-dropdown-menu-through-pdo-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!