如何在查询和显示表中的数据时使用从PHP函数返回的值?

时间:2016-05-04 22:09:48

标签: php mysql

我试图在课堂上显示多名学生的测试结果。为此,我使用函数find_student_by_year($ year)来获取包含学生ID的所有学生的列表。然后在sql语句中使用id来查询特定学生ID的结果。我的代码不会抛出错误,但即使表实际上有数据也不显示数据。不幸的是,我的研究没有显示任何相似之处,因此我的帖子中没有添加链接。请参阅下面的代码

功能

function find_student_by_year($year) {
global $connection;

$query = "select * from students where entry_year = {$year} order by s_fname asc";
$found_students = mysqli_query($connection, $query);
confirm_query($found_students);
return $found_students;
}

PHP和HTML(混合)代码

<tbody>

<?php 
    $staff_id = 27;
    $subject_id = 2;

    //Actually getting this value from $_GET but am hard coding it here
    $year = 4;

    if (isset($year)) { 
        $student_year = find_student_by_year($year); 
    } else {
        $student_year = null;
    } 

    if (isset($student_year)) {
        while ($year_group = mysqli_fetch_assoc($student_year)) { 
?>

<tr>
    <td><input name="student_id[]" value="<?php echo htmlentities($year_group["student_id"]) ?>" readonly></td>
    <td><?php echo $year_group["s_fname"] . " " . $year_group["s_mname"] . " " . $year_group["s_lname"]; ?></td>

    <?php
        $result = mysqli_query($connection, "select * from results where student_id = {$year_group['student_id']} and subject_id = {$subject_id}");
        while($result_record = mysqli_fetch_assoc($result)) {
    ?>

    <td style="width: 15%"><input type="text" name="test1[]" class="form-control" style="text-align: center" 
        value="<?php if ($result_record['test1'] !== ' ') { echo $result_record['test1']; } else {echo ' ';} ?>">
    </td>
    <td style="width: 15%"><input type="text" name="test2[]" class="form-control" style="text-align: center" 
        value="<?php if ($result_record['test2'] !== ' ') { echo $result_record['test2']; } else {echo ' ';} ?>">
    </td>
    <td style="width: 15%"><input type="text" name="test3[]" class="form-control" style="text-align: center" 
        value="<?php if ($result_record['test3'] !== ' ') { echo $result_record['test3']; } else {echo ' ';} ?>">
    </td>
    <td style="width: 15%"><input type="text" class="form-control" style="text-align: center" 
        value="<?php echo htmlentities($result_record['final']); ?>" readonly>
    </td>
</tr>
<?php } } } ?>

</tbody>

查看数据库表并显示屏幕截图

Database screenshot

HTML display

2 个答案:

答案 0 :(得分:0)

如果您尝试使用此代码会发生什么:

function find_student_by_year($year) {
   global $connection;
    $query = "select * from students where entry_year = '$year' order by 
    s_fname asc";

   $found_students = mysqli_query($connection, $query);

   confirm_query($found_students);    
   return $found_students;

}

答案 1 :(得分:0)

使用student_id列在学生和记录表上使用join语句创建视图。然后查询视图并使用student_id显示每个学生的结果。请参阅下面的代码提取

<?php
$subject_id = 2;
if (isset($_GET["year"])) {
    $result = mysqli_query($connection, "select * from fullresult where year = {$_GET["year"]} and subject_id = {$subject_id}");
    while($student_result = mysqli_fetch_assoc($result)) {
?>
<tr>
<td style="display: none"><input name="student_id[]" value="<?php echo htmlentities($student_result["student_id"]) ?>"></td>
<td><?php echo $student_result["firstname"] . " " . $student_result["middlename"] . " " . $student_result["lastname"]; ?></td>
<td style="width: 15%; text-align: center">
    <?php 
        if ($student_result['test1'] !== ' ') {
            echo $student_result['test1']; } else {
                echo '<input type="text" name="test1[]" class="form-control" style="text-align: center" value="">';
            }
    ?>
</td>
<td style="width: 15%; text-align: center">
    <?php 
        if ($student_result['test2'] !== ' ') {
            echo $student_result['test2']; } else {
                echo '<input type="text" name="test2[]" class="form-control" style="text-align: center" value="">';
            }
    ?>
</td>
<td style="width: 15%; text-align: center">
    <?php 
        if ($student_result['test3'] !== ' ') {
            echo $student_result['test3']; } else {
                echo '<input type="text" name="test3[]" class="form-control" style="text-align: center" value="">';
            }
    ?>
</td>
<td style="width: 15%; text-align: center"><?php echo htmlentities($student_result['total']); ?></td>
</tr>
<?php 
  } 
} 
?>

Students table structure

Records table structure

Results view table structure

Front end display