尝试允许php使用相同的变量查询来提取“代码”和“前缀”字段。
错误:未显示'code'
<script type="text/javascript">
$(document).ready(function() {
$("#ProfFields,#courseFields").hide();
$("input:radio[name='addType']").click(function(){
var profFields = ($(this).val()=="Professor");
$("#ProfFields").toggle(profFields);
$("#courseFields").toggle(!profFields);
});
});
</script>
<?php
require_once('inc/dbc1.php');
$pdo = new PDO('mysql:host=ureviewdu.db.6511651.hostedresource.com;dbname=ureviewdu', $username, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
SELECT name
FROM Department
;');
$sth->execute(array());
$pdo2 = new PDO('mysql:host=####;dbname=###', $username, $password);
$pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth2 = $pdo2->prepare('
SELECT prefix, code FROM Course
;');
$sth2->execute(array());
?>
<div id="popup_name" class="popup_block">
<h2 style="padding:0; margin:0;">Add a:</h2><br>
<form action="inc/add_p_c_validate.php" method="post" id="addition">
Professor<input type="radio" name="addType" value="Professor" />
Course<input type="radio" name="addType" value="Course" />
<div id="courseFields">
Course[Prefix]: <select name="cPrefix" id="cPrefix" style="width:150px;" />
<?php while($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row2['prefix']." "."</option><br>";} ?> </select>
Course[Code]: <select name="cCode" id="cCode" style="width:150px;" />
<?php while($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row2['code']." "."</option>";} ?></select>
Department: <select name="deptName" id="deptName" style="width:350px;">
<?php while($row = $sth->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row['name']." "."</option>";} ?></select>
Email: <input type="text" name="email" class="l" />
</div><!--/courseFields-->
<div id="ProfFields">
<br>First Name: <input type="text" name="name" style="width:385px;" /><br>
<br>Last Name: <input type="text" name="name" style="width:385px;" /><br>
Department: <select name="deptName" id="deptName" style="width:350px;">
</div><!--/ProfFields-->
<input type="submit" name="submit" />
</form>
</div><!--popup_name-->
答案 0 :(得分:3)
在第36行,您忘记用?>
关闭PHP标记。
<?php while($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row2['prefix']." "."</option>"; ?>
你必须这样做:
<?php while($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row2['prefix']." "."</option>"; ?>
Course[Code]: <select name="cName" id="cName" style="width:150px;" />
<?php while($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {echo "<option>".$row2['code']." "."</option>";}
} ?></select>
如果您想回显代码,那么您只需在其中使用另一个<?php echo ... ?>
。
答案 1 :(得分:0)
您尝试迭代结果集两次而不在中间重置它(实际上,PDO不支持通过结果集进行任意搜索)。 Cannae做到了,队长。
尝试迭代结果集一次(同时为两个<select>
元素构建HTML):这意味着只有一个 while
循环涉及$sth2
。
<?php
$prefixes = Array();
$codes = Array();
while ($row2 = $sth2->fetch(PDO::FETCH_ASSOC)) {
$prefixes[] = $row2['prefix'];
$codes[] = $row2['code'];
}
?>
Course[Prefix]: <select name="cPrefix" id="cPrefix" style="width:150px;" />
<?php foreach ($prefixes as $prefix) { echo "<option>$prefixes.</option><br>";} ?>
</select>
Course[Code]: <select name="cCode" id="cCode" style="width:150px;" />
<?php foreach ($codes as $code) { echo "<option>$code.</option>";} ?>
</select>