php mysql选择下拉多个相关表

时间:2015-12-19 21:13:51

标签: php mysql

目前我有2张桌子,只能使用PHP(没有javascript)。

table category:

id, name
1, mymother
2, hismother

table subcategory:

id, name, cat_id
1, cool, 2
2, uncool, 1
3, milf, 2
4, ugly, 1

首先,您可以将它们组合在一个下拉框中 所以结果将是:

mymother
 -uncool
 -ugly
hismother
 -cool
 -milf

只有子类别可选。

第二,可以将类别/子类别再次拆分为2个原始值,再次重新发布到mysql并插入表nmbr.3

table parents:
id, phonenmbr, cat_id, subcat_id 

对于单个值,这很简单:

$result = $mysqli->query("SELECT id, name FROM category");
echo "<select name='category'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name']        

"</option>";

为了再次组合AND拆分category_id和sub_category_id,我想我需要做两个单独的sql查询而不是使用JOIN ....但不确定

1 个答案:

答案 0 :(得分:0)

可能在代码下方可以让您了解自己需要做什么:

    <form method="get" action="example.php">

            <select id="cd" name="cd">

                <?php

                $mysqlserver="localhost";
                $mysqlusername="test";
                $mysqlpassword="test";
                $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

                $dbname = 'test';
                mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

                $cdquery="SELECT cdTitle, cdArtist, cdReference FROM firsttable";
                $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());

               while ($cdrow=mysql_fetch_array($cdresult)) {
            $cdTitle=$cdrow["cdTitle"];
            $cdArtist=$cdrow["cdArtist"];
            $cdReference=$cdrow["cdReference"];
             echo "<option value=\"$cdReference\">
            $cdTitle $cdArtist
            </option>";
           }

                ?>

            </select>

        </form>