从mysql中显示php中的表格

时间:2015-05-22 06:13:56

标签: php html mysql

我正在做一个搜索引擎,在文本字段中插入学生ID之后,它将显示有关学生的数据。我想使用php代码以表格形式显示它。但它不起作用。以下是我的代码。

<html> 
<head> 
    <title>MrBool Development Article</title> 
</head> 
<body> 
    <form action="search.php" method="GET"> 
        <b>Enter Search Term:</b> 
        <input type="text" name="term" size="50"> 
        <input type="submit" value="Search"> 
    </form> 

    <?php 
        mysql_connect("localhost", "root", ""); 
        mysql_select_db("attendance_system"); 
        $sql = mysql_query("SELECT * FROM studentAttendance WHERE student_stud_matric LIKE '%$_GET[term]%'"); 
        while ($ser = mysql_fetch_array($sql)) {
            echo "<table>
                    <thead>
                        <tr>
                            <th>Student Matric</th>
                            <th>Subject Name</th>
                        </tr>
                    </thead>                                        
                    <tbody>";   

            echo "<tr>
                    <td>".$ser[student_stud_matric]."</td>
                    <td>".$ser[course_course_code]."</td>                                       
                </tr>";

            echo "</tbody></table>";
        } 
    ?> 
    <hr> 
    <a href="index.php">Go Back</a> 
</body> 
</html>

2 个答案:

答案 0 :(得分:0)

不要在循环中创建表格,就像这样

<!-- enctype="multipart/form-data" -->
<form id="uploadForm"
    enctype="multipart/form-data"
    action="/file/upload"
    method="post">
        <input type="file" name="uploadFile" />
        <input type="submit" value="submit"/>
</form>

答案 1 :(得分:0)

将以下内容替换为while循环,即可完成。但是,要认真考虑避免来自莫名的mysql_*:)

$table = "<table><thead><tr><th>Student Matric</th><th>Subject Name</th></tr></thead><tbody>";
while($ser = mysql_fetch_array($sql)) {
    $table .= "<tr><td>{$ser['student_stud_matric']}</td><td>{$ser['course_course_code']}</td></tr>";
}
$table .= "</tbody></table>";
echo $table;