无法从数据库中显示表

时间:2015-04-19 10:22:13

标签: php html mysql html-table

无法从数据库中显示该表。对于每个程序,都有一个表,其中保存了一些分数。此外,还有一个程序表,其中保存了program idprogram name。当我选择一个程序和一年时,我得到以下错误:

  

未找到数据

     

注意:未定义的变量:导致第61行的C:\ XAMPP \ htdocs \ statistics \ ac_directorPrograms.php

     

警告:mysql_fetch_assoc()期望参数1为资源,在第61行的C:\ XAMPP \ htdocs \ statistics \ ac_directorPrograms.php中给出null

操作代码中的result出现问题,我想表格形式为prog_nameprogram

表格:

   <?php 
      include 'connect.php'; 
   ?>
    <form name="myform" action="ac_directorPrograms.php" method="POST">

      <b>Programs:<b/>
        <select name="program"> 
        <option value="Choose">Please select..</option>
        <?php
                $sql = mysql_query("SELECT prog_name FROM program");
                while ($row = mysql_fetch_array($sql)) {
                echo "<option value='" . $row['prog_name'] . "'>" .    $row['prog_name'] ."</option>";
                }
        ?>  
         </select><br/><br/>

<b>Year:<b/>
<select name="year"> 
<option value="Choose">Please select..</option>
<option value="2005">2005</option> 
<option value="2006">2006</option>
<option value="2007">2007</option></select><br/><br/>


<br/>
<input type="submit" value="submit" name="Submit">
<input type="reset" name="reset" value="Clear">

</form>
</div>

动作代码

      <?php 

        include 'connect.php';

        $year = $_POST['year'];
        $program = $_POST['program']; 
        $years     = array(
            2005,
            2006,
            2007
       );
       $programs = array(
          'bsc computer science',
          'bsc psychology',
          'ba finance',
          'ba marketing',
          'ba management'
       );

       if (in_array($program, $programs) && in_array($year, $years)) {

          $sql = "SELECT a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $program WHERE year=$year";

            $result = mysql_query($sql);
      }

      else {
            echo "No data found";
     }
     ?>
        <html>
         <head>
         <link rel="stylesheet" type="text/css" href="../../statistics/style.css">
         </head>
          <body>

           <div id="container">
            <table id="table" width="900" border="1" cellspacing="1">

             <tbody>
               <tr>
                <td>A1 </td>
                <td>A2 </td>
                <td>A3 </td>
                <td>L1 </td>
                <td>L2 </td>
                <td>L3 </td>
                <td>L4 </td>
                <td>L5 </td>
                <td>L6 </td>
                <td>L7 </td>
                <td>LAVG </td>
                 <td>R1 </td>
                <td>R2 </td>
                <td>U1 </td>
                <td>U2 </td>
               <td>U3 </td>
         </tr>
       </tbody>
       <?php
              while($program=mysql_fetch_assoc($result)){
                   echo "<tr>";
                   echo "<td>".$program['a1']."</td>";
                   echo "<td>".$program['a2']."</td>";
                   echo "<td>".$pprogram['a3']."</td>";
                   echo "<td>".$pprogram['l1']."</td>";
                   echo "<td>".$program['l2']."</td>";
                   echo "<td>".$program['l3']."</td>";
                   echo "<td>".$program['l4']."</td>";
                   echo "<td>".$program['l5']."</td>";
                   echo "<td>".$program['l6']."</td>";
                   echo "<td>".$program['l7']."</td>";
                   echo "<td>".$program['lavg']."</td>";
                   echo "<td>".$program['r1']."</td>";
                   echo "<td>".$program['r2']."</td>";
                   echo "<td>".$program['u1']."</td>";
                   echo "<td>".$program['u2']."</td>";
                   echo "<td>".$program['u3']."</td>";
                  echo "</tr>";    
               }
            ?>
      </table>
    </div>
   </body>
   </html>

2 个答案:

答案 0 :(得分:1)

在操作代码中和include connect.php之后,添加此内容... $con=mysqli_connect("localhost","my_user","my_password","my_db");并且不要忘记用您的用户,密码和数据库名称替换my_usermy_passwordmy_db

同样将此代码$result = mysql_query($sql);替换为$result=mysqli_query($con,$sql);

答案 1 :(得分:0)

这是变量范围的问题。你已经在条件中定义了$ result并使用了if的那一边。你需要在if条件下定义$ result out。