dongsu2807
2014-04-11 01:57
浏览 45
已采纳

如何根据php中的下拉列表检索数据?

Ive been having problem with my codes.. Im a still noob with php so i really appreciate if anyone could help me :'( .. Im trying to display data from database based on the selected dropdown list value. There are three data currently in my database table .. However, only one show up on my output .. i dont know whats wrong with my codes. can anyone help me ..

this is my code departmentlist.php

Choose Department : 

     <form action="" method="POST">
     <select name="department">
        <option value="" selected="selected">-- Choose department --</option>
       <option value="Information System Department">Information System Department</option>
      <option value="Finance">Finance</option>
      <option value="HR and Administration">HR and Administration</option>
      <option value="Security">Security</option>

        </select>

     <input type="submit" value="Submit" />
    </form>
    </br>

    <table border="1" id="table">

     <tr><th bgcolor="#00CCCC">Department</th>
     <th bgcolor="#00CCCC">Locaion</th>
     <th bgcolor="#00CCCC">Serial Number</th>
     <th bgcolor="#00CCCC">Description of asset</th>

   </tr>
        <?php 
        mysql_connect('localhost','root',"") or die('Error1 '.mysql_error());

        mysql_select_db("ams") or die('error2'.mysql_error());


          if($_SERVER['REQUEST_METHOD'] =='POST')
     { $dep=$_POST['department'];
         $query="SELECT * FROM asset WHERE department= '" . $dep . "'";
         $run=mysql_query($query);
         $numrow = mysql_num_rows($run);


         $row=mysql_fetch_array($run, MYSQLI_ASSOC);

        echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";

    }

    }
    ?>
    </table>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongqi0644 2014-04-11 02:08
    已采纳

    Manual: http://www.php.net/mysql_fetch_array

    Put mysql_fetch_array in a while loop so:

    while ($row = mysql_fetch_array($run, MYSQL_ASSOC)) {
       echo "<tr><td bgcolor='#00FFCC'>".$row['department']."</td><td bgcolor='#00FFCC'>".$row['location']."</td><td bgcolor='#00FFCC'>".$row['serialno']."</td><td bgcolor='#00FFCC'>".$row['desc']."</td></tr>";
    }
    

    I also suspect that you should MYSQL_ASSOC not MYSQLI_ASSOC since you're using mysql not mysqli.

    Take note that mysql will be deprecated so use mysqli.

    点赞 评论
  • doutang3077 2014-04-11 02:06

    you have to loop through the results.

    while($row = mysql_fetch_array($run, MYSQLI_ASSOC)){
    
        echo "<tr><td bgcolor='#00FFCC'>".$row['department']
            ."</td><td bgcolor='#00FFCC'>".$row['location']
            ."</td><td bgcolor='#00FFCC'>".$row['serialno']
            ."</td><td bgcolor='#00FFCC'>".$row['desc']
            ."</td></tr>";
    }
    
    点赞 评论
  • douchun9719 2014-04-11 02:08

    Try using a while loop.

                while($row=mysql_fetch_array($run)){
                    echo "<tr>
                            <td bgcolor='#00FFCC'>".$row['department']."</td>
                            <td bgcolor='#00FFCC'>".$row['location']."</td>
                            <td bgcolor='#00FFCC'>".$row['serialno']."</td>
                            <td bgcolor='#00FFCC'>".$row['desc']."</td>
                        </tr>"; 
                }
    
    点赞 评论

相关推荐 更多相似问题