dongqiao0953 2016-06-28 23:08
浏览 42

数据库中的Mysql PHP多层下拉列表

Hello Help Much Appreciated. I am having trouble figuring this out. I cannot find tutorials anywhere with this specific task. I have one table with ID, Manufacturer, ModelNo, Manupartcode and more fields.

I want first drop down list to be populated with Manufacturer. SELECT Manufacturer FROM Table_Name; This is fine

The second list need to be ModelNo. SELECT ModelNo FROM Table_Name WHERE Manufacturer = 'What We picked from first list';

INDEX.PHP

<!DOCTYPE html>
<html>
<head>
<script>
function showData(str) 
{
    if (str == "") 
    {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } 
    else 
    { 
        if (window.XMLHttpRequest) 
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } 
        else 
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() 
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
            {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","showData.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
<?php

$conn = new mysqli('Server_Name', 'Username', 'Password', 'Database_Name') 
or die ('Cannot connect to db');

    $result = $conn->query("select DISTINCT Manufacturer from Table_Name");

    echo "<select name = 'Manufacturer' onchange = 'showData(this.value)'>";

    while ($row = $result->fetch_assoc()) {

                  unset($Manufacturer);
                  $Manufacturer = $row['Manufacturer']; 
                  echo '<option value="'.$Manufacturer.'">'.$Manufacturer.'</option>';

}

    echo "</select>";
?>
</form>

<br>

<div id="txtHint">
    Text
</div>

</body>
</html>


SHOWDATA.PHP

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
    padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('Server_Name', 'Username', 'Password', 'Database_Name');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"Database_Name");
$sql="SELECT ModelNo FROM Table_Name WHERE Manufacturer = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>ModelNo</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['ModelNo'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • donglun4521 2016-06-29 00:46
    关注

    Solved

    Found this online here somewhere.

    Swapped this in: echo '<option value="'.$Manufacturer.'">'.$Manufacturer.'</option>';

    Added this instead:

    $q = isset( $_GET['q'] ) ? $_GET['q'] :'WW';
    

    The query works fine now.

    $sql="SELECT ID, ModelNo FROM IE_STARSYSIE_stock WHERE Manufacturer = '".$q."'";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗