在选择菜单中获取mysql数据库的值

时间:2013-11-21 04:09:05

标签: php mysql session relational-database

我在PHP上有选择菜单的问题。我试图让mysql数据库选择菜单。但是,它没有显示。

这是我的代码:                         默认值:

        mysql_select_db($database_conn, $conn);
        $query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
        $Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
        $row_Rsenroll = mysql_fetch_assoc($Rsenroll);
        $totalRows_Rsenroll = mysql_num_rows($Rsenroll);
   $courseid = $row_Rsenroll['courseid'];
   $er_staffid = "";

        break;
        }
    ?>

        <select name="courseid">
        <option value="" SELECTED>Selected Course ID</option>
          <?php 
    foreach( $Course as $course_id) {
        if ( $course_id == $courseid) {
            $selected = " SELECTED";
        } else {
            $selected = "";
        }
            ?>
         <option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
         <?php
    }
    ?>
        </select>

感谢您提供任何帮助和建议。

2 个答案:

答案 0 :(得分:0)

假设courseid作为发送URL中的变量传递(file.php?courseid = COURSEID),我认为这应该做你想要的:

这可能会稍微清理你的脚本(虽然我把它切换到了mysql_fetch_array,因为我比mysql_fetch_assoc更熟悉。可以随意使用assoc):

   <?php
   $cid = '6116';
   ?>


   <select name="courseidMenu">
   <option value="" SELECTED>Selected Course ID</option> 
   <?php

   $query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
   $total_rows = mysql_num_rows($query);
   while($row = mysql_fetch_array($query)){
      $courseId = $row['courseid'];
      ?>
      <option value="<?=$courseId?>" ><?=$courseId?></option>      
      <?

   }
   ?>
   </select>

答案 1 :(得分:0)

updated    use this it is working on my portal                           <select>
        <option value=''>Select Provider</option>
        <?php
        $server="server name";
        $user="user name";
        $password="password";
        $database="database";
        $conn=mysql_connect($server,$user,$password) or die("connection failed");
        mysql_select_db($database,$conn);
        $query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
        $result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
        $n=mysql_num_rows($result);
        if($n>0)
        while($row=mysql_fetch_array($rs))
        echo"<option value='$row['courseid']'>$row['courseid']</option>";
        mysql_close($conn);
        ?>