注意:未定义的变量:table in C:\XAMPP\htdocs\progscores.php on line 27

Notice: Undefined variable: table in C:\XAMPP\htdocs\progscores.php on line 27

我有一个带有下拉菜单的表单。每当我select一个节目时,系统都会显示该节目的分数table。但是当我 select 服务名称时,它不会显示服务 table。另外,有没有更好、更快、更有效的方法来显示数据库中的 tables?

这是我的表格:

我得到的错误:

progscores.php

  <?php

    include'connect.php';

    $crit=@$_POST['crit'];
    $y2005=@$_POST['y2005'];
    $y2006=@$_POST['y2006'];
    $y2007=@$_POST['y2007'];
    $y2008=@$_POST['y2008'];
    $y2009=@$_POST['y2009'];
    $y2010=@$_POST['y2010'];

    switch($_POST['program'])
    {
          case 'Computer Science':
             $table = 'csprog';
             break;
          case 'BAED':
            $table = 'baedprog';
             break;
          case 'Psychology':
             $table = 'psyprog';
             break;

      }

     $res = mysql_query("SELECT * FROM {$table}");

 ?> 

  <html>
  <body>
  <table width="600" border="1" cellspacing="1">
    <tr>
         <th>Criteria</th>
         <th>2005</th>
         <th>2006</th>
         <th>2007</th>
         <th>2008</th>
         <th>2009</th>
         <th>2010</th>
   </tr>

    <?php
       while($table=mysql_fetch_assoc($res)){

         echo "<tr>";

         echo "<td>".$table['crit']."</td>";

         echo "<td>".$table['y2005']."</td>";

         echo "<td>".$table['y2006']."</td>";

         echo "<td>".$table['y2007']."</td>";

         echo "<td>".$table['y2008']."</td>";

         echo "<td>".$table['y2009']."</td>";

         echo "<td>".$table['y2010']."</td>";

         echo "</tr>";

      }
   ?>
  </table>
  </body>
  </html>


  principal.php

      <?php

          include 'connect.php';

          if($_POST['program']){
             include 'progscores.php';

          }elseif($_POST['services']){
             include 'servscores.php';
          }

        ?> 


   servscores.php


         <?php

            include'connect.php';

            $scrit=@$_POST['scrit'];
            $sy2005=@$_POST['sy2005'];
            $sy2006=@$_POST['sy2006'];
            $sy2007=@$_POST['sy2007'];
            $sy2008=@$_POST['sy2008'];
            $sy2009=@$_POST['sy2009'];
            $sy2010=@$_POST['sy2010'];

            switch($_POST['services'])
            {
                case 'Library':
                   $stable = 'library';
                   break;
                case 'Course Administrator':
                   $stable = 'cadmin';
                   break;
               case 'Front Desk':
                   $stable = 'frontdesk';
                   break;
               case 'Clubs and Societies':
                   $stable = 'clubs';
                   break;
              case 'IT Services and Facilities':
                   $stable = 'itservice';
                   break;
              case 'International Student Office':
                  $stable = 'stoffice';
                   break;
          }

          $res2 = mysql_query("SELECT * FROM {$stable}");

       ?> 

         <html>
         <body>
            <table width="600" border="1" cellspacing="1">
              <tr>
                   <th>Criteria</th>
                   <th>2005</th>
                   <th>2006</th>
                   <th>2007</th>
                   <th>2008</th>
                   <th>2009</th>
                   <th>2010</th>
              </tr>

        <?php
             while($stable=mysql_fetch_assoc($res2)){

              echo "<tr>";

              echo "<td>".$stable['scrit']."</td>";

              echo "<td>".$stable['sy2005']."</td>";

              echo "<td>".$stable['sy2006']."</td>";

              echo "<td>".$stable['sy2007']."</td>";

              echo "<td>".$stable['sy2008']."</td>";

              echo "<td>".$stable['sy2009']."</td>";

              echo "<td>".$stable['sy2010']."</td>";

             echo "</tr>";

         }
       ?>

       </table>
       </body>
       </html>

$table 未定义,因为您直接在 switch 代码块中声明它。所以首先你必须声明

$table=''; 

切换前

因为 $table 未定义所以出现第二个警告