Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在表单中从SQL访问信息_Php_Mysql_Loops - Fatal编程技术网

Php 在表单中从SQL访问信息

Php 在表单中从SQL访问信息,php,mysql,loops,Php,Mysql,Loops,我有一个类似这样的查询(我知道它容易受到sql注入的攻击,它只是一个模板): 那么我有这个表格: <form name="form" id="form" method="post" action="page"> <select name="student" id="student"> <? for ($i=0;$i<=$numRows;$i++){ echo "<option>" . $students['StudentID'] . "

我有一个类似这样的查询(我知道它容易受到sql注入的攻击,它只是一个模板):

那么我有这个表格:

<form name="form" id="form" method="post" action="page">
<select name="student" id="student">

<? 
for ($i=0;$i<=$numRows;$i++){
    echo "<option>" . $students['StudentID'] . "</option>";

}

?>


  <option selected="selected">Please select a student</option>
   </select>

请选择一名学生

这应该打印一个类似于下拉框的内容,其中表中的所有值都是项目。但是,我得到了一个包含6个相同项目的下拉框,在本例中,我得到了6个学生Id,但它们都是相同的Id。我在db中有6个学生,我希望将每个学生都显示为一个选项。我做错了什么


TIA

请看以下示例。它将只返回一条记录。为了获得所有这些,您必须在循环中使用它,例如:

$students = array();

while(($student = mysql_fetch_assoc($result))) {
    $students[] = student;
}
后来:

<?php foreach($students as $student): ?>
    <option><?php echo $student['StudentID']; ?></option>
<?php endforeach; ?>

(注意:我使用了imho,当混合HTML和PHP时,imho的可读性更高)

看一下示例。它将只返回一条记录。为了获得所有这些,您必须在循环中使用它,例如:

$students = array();

while(($student = mysql_fetch_assoc($result))) {
    $students[] = student;
}
后来:

<?php foreach($students as $student): ?>
    <option><?php echo $student['StudentID']; ?></option>
<?php endforeach; ?>
(注意:我使用的是imho,当混合HTML和PHP时,它的可读性更高)