dpcj40970 2015-06-03 12:34
浏览 21
已采纳

使用Php下拉值选择mysql表

I am new to php...i need help. I have 5 tables in my sql database, I would like to create a dropdown that basing on the value of the dropdown, then a specific table/ view is selected..please guide me.. Thank you.

Below is my php script..

DATA QUERY By: Country District County/ Municipality Sub-County/ Municipality

//select database
MySQL_SELECT_db('edudata',$getconnected);

//creating query to excute on the MySQL dbms
$query="SELECT * FROM ;";
// Excute the query above
$erecords=MySQL_Query($query, $getconnected); 

echo '<table align="center" cellspacing="3" bgcolor="#d3dbde"     cellpadding="3" width="70%"> <tr><td align="left"><b>District</b></td><td align="left"><b>County/ Municipality</b></td><td align="left"><b>Number of Schools</b></td><td align="left"><b>Number of Schools</b></td><td  align="left"><b>Number of Female Teachers
</b></td> <td align="left"><b>No. of Male Teachers</b><td align="left">  <b>Female Enrollment</b></td><td align="left"><b>Male Enrollment</b></td>  </tr>';

While ($emprecord=MySQL_fetch_array($erecords))


{
Echo '<tr bgcolor="#f6f6f6"><td align="left">'.$emprecord['District'].    '</td><td align="left">'. $emprecord['County_Municipality']. '</td><td   align="left">'. $emprecord['No_of_schools']. '</td><td align="left">'.   $emprecord['Female_Teachers']. '</td><td align="left">'.   $emprecord['Male_Teachers'].'</td><td align="left">'.   $emprecord['Female_Enrolment'].'</td><td align="left">'.    emprecord['Male_Enrollment'].'</td></tr>';


}
echo '</tables>';



MySQL_Close($getconnected);

?>

After Applying your solution, with code below, In get the errors stated in the comments.

<?php 
 $getconnected=mysql_connect('localhost','**','**', 'edudata');
 if(isset($_POST['submit']))

{
  $mytableselect=$_POST['mytableselect'];
 //select database
//MySQL_SELECT_db('edudata',$getconnected);
mysql_select_db('edudata', $getconnected);
//creating query to excute on the MySQL dbms
$query="SELECT * FROM $mytableselect ";
// Excute the query above
$erecords=mysql_query($query, $getconnected); 

echo '<table align="center" cellspacing="3" bgcolor="#d3dbde"     cellpadding="3" width="70%"> <tr><td align="left"><b>District</b></td><td align="left"><b>County/ Municipality</b></td><td align="left"><b>Number of Schools</b></td><td align="left"><b>Number of Schools</b></td><td  align="left"><b>Number of Female Teachers
</b></td> <td align="left"><b>No. of Male Teachers</b><td align="left">  <b>Female Enrollment</b></td><td align="left"><b>Male Enrollment</b></td>  </tr>';

While ($emprecord=MySQL_fetch_array($erecords))



//Echo'<tr bgcolor="#f6f6f6"><td align="left">'.$emprecord['District'].'</td><td align="left">'. $emprecord['County_Municipality']. '</td><td   align="left">'. $emprecord['No_of_schools']. '</td><td align="left">'.   $emprecord['Female_Teachers']. '</td><td align="left">'.$emprecord['Male_Teachers'].'</td><td align="left">'.$emprecord['Female_Enrolment'].'</td><td align="left">'.emprecord['Male_Enrollment'].'</td></tr>';
Echo '<tr bgcolor="#f6f6f6"><td align="left">'.$emprecord['District']. '</td><td align="left">'. $emprecord['County_Municipality']. '</td><td align="left">'. $emprecord['No_of_schools']. '</td><td align="left">'. $emprecord['Female_Teachers']. '</td><td align="left">'. $emprecord['Male_Teachers'].'</td><td align="left">'. $emprecord['Female_Enrolment'].'</td><td align="left">'. $emprecord['Male_Enrollment'].'</td></tr>';

echo'</tables>';



MySQL_Close($getconnected);

}

?>
  • 写回答

2条回答 默认 最新

  • dongniuxia8650 2015-06-09 08:52
    关注

    This worked the magic for me.

    <?php
    $dbLink = mysqli_connect('localhost', 'usr', 'passd');
    mysqli_select_db('edudata', $dbLink);
    $mytableselect=$_POST['mytableselect'];
    $sql = "SELECT * FROM $mytableselect";
    $result = mysqli_query($sql) or die(mysqli_error());
    
    // Print the column names as the headers of a table
    echo "<table><tr>";
    for($i = 0; $i < mysqli_num_fields($result); $i++) {
        $field_info = mysqli_fetch_field($result, $i);
        echo "<th>{$field_info->name}</th>";
    }
    
    // Print the data
    while($row = mysqli_fetch_row($result)) {
        echo "<tr>";
        foreach($row as $_column) {
            echo "<td>{$_column}</td>";
        }
        echo "</tr>";
    }
    
    echo "</table>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?