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 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器