通过PDO代码填充下拉菜单 [英] populating dropdown menu through pdo code
问题描述
好吧,所以我只盯着pdo,但是慢慢地掌握了它,我想知道如何制作下拉菜单或列表pox,将数据填充到页面中我已经开始编写代码的字段中通过查找pdo指南等,但是即时通讯无法为此找到解决方案.也为代码不整洁而感到抱歉,但我还是整个编程领域的新手
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');
?>
其中还包括我的会话cookie,但效果很好,这是我到目前为止所拥有的下拉框的数量.
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>
我要填充的文本框是txt_exam_id,txt_location,txt_date_taken,txt_exam_taken,txt_grade_recieved
the text boxes i am trying to poulate are txt_exam_id, txt_location, txt_date_taken, txt_exam_taken, txt_grade_recieved
推荐答案
答案很简单:不要通过pdo代码填充下拉菜单
这是完全不同的事情,永远不要混入代码中.
That's totally different matters which should never be intrmixed in the code.
将代码分为两部分:
- PDO代码
- 从常规数组代码填充任何菜单.
分别编写和调试这些部分.
write and debug these parts separately.
$smt = $connection->prepare('select exam_id From exam');
$smt->execute();
$data = $smt->fetchAll();
现在,您的考试存储在$ data数组中.
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>
这篇关于通过PDO代码填充下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!