使用PHP POST方法输入值后,HTML表格不会更新

时间:2018-04-10 18:54:41

标签: php html database post

我有一个包含课程ID及其相应课程名称的数据库。我尝试使用下拉菜单创建过滤器,过滤显示的课程。

这是代码 -

<body>
<div id="wrapper">
<?php // sqltest.php

include 'header.php';
include 'nav.php';

require_once 'login.php';

$conn = mysqli_connect($hn, $un, $pw, $db);
if (!$conn) {
 die ('Fail to connect to MySQL: ' . mysqli_connect_error());   
}


echo '<form action="" method="post">
<select name="Course Name">
<option value="All" selected="selected">All course names</option>
<option value="Introduction to Data Science">Introduction to Data Science</option>
<option value="Database Management Systems">Database Management Systems</option>
<option value="Data Visualization">Data Visualization</option>
</select>
<input type="submit" value="Search" name="submit" />
</form>';

$cname = isset($_POST['Course Name']) ? $_POST['Course Name'] : 'All';

if(isset($_POST)) {

 if ($cname == 'Introduction to Data Science') {
    $query = "SELECT * FROM courses WHERE Cname='Introduction to Data'";
 } elseif ($cname == 'Database Management Systems') {
    $query = "SELECT * FROM courses WHERE Cname='Database Management'";
 } elseif ($cname == 'Data Visualization') {
    $query = "SELECT * FROM courses WHERE Cname='Data Visualization'";
 } elseif ($cname == 'All') {
    $query = "SELECT * FROM courses";
 }

 $result = mysqli_query($conn, $query);

 if (!$result) {
    echo 'Could not get data: ' . mysqli_error($conn);
 }
 echo '<br>Available courses for hardcoded student 000-01-0002<br><br>';

 echo '<table>
 <thead>
    <tr>
        <th>Course ID</th>
        <th>Course Name</th>
        <th>Add</th>
    </tr>
 </thead>
 <tbody>';

 while ($row = mysqli_fetch_array($result)) {
    echo '<tr>
    <td>' . $row['cid'] . '</td>
    <td>' . $row['Cname'] . '</td>
    <td><a href="addClass.php?Cid=' . $row['cid'] . '">Add</a></td>
    </tr>';
  }
 }


echo '
</tbody>
</table>
</body>';

它会在页面首次加载时显示所有课程,但选择任何一个课程并单击“提交”都不会更改初始表。

1 个答案:

答案 0 :(得分:1)

删除课程名称帖子字段中的空格。由于空间的原因,它未被识别为isset检查中的有效POST字段。

将其命名=&#34; course_name&#34;或选择字段上的某些内容。