简体   繁体   English

如何从下拉列表中选择值,如何从另一个表中获取相应的值,然后将其放入另一个下拉列表中?

[英]How to select value from a drop-down list, get the corresponding values from another table, then put those into another drop-down list?

Ok, here's a database. 好的,这是一个数据库。

http://i.stack.imgur.com/j05AB.png http://i.stack.imgur.com/j05AB.png

Say I've inserted values into the database for each of these tables, although the IDs would be auto incrementing. 假设我已将这些表中的每个表的值插入数据库中,尽管ID会自动递增。 There are many BVALUES from each AVALUE, thus the AB table. 每个AVALUE中都有许多BVALUE,因此是AB表。 I have all the AVALUEs from TABLE A in a drop-down list. 我在下拉列表中具有表A的所有AVALUE。 A user selects an AVALUE which I put into a variable using 用户选择一个AVALUE,我使用

$AVALUE = $_POST['AVALUE']

Then I do an sql statement to get the AVALUEs from TABLE A that equal $AVALUE. 然后,我执行一条sql语句从表A中获取等于$ AVALUE的AVALUE。

$sql = "SELECT AVALUE FROM TABLEA WHERE" . $AVALUE . " = AVALUE";

How do I then get the NAMEID from TABLEA for that AVALUE, then reference to AB where TABLEANAMEID = NAMEID from TABLEA? 然后,我如何从TABLEA中获得该AVALUE的NAMEID,然后从TABLEA中引用AB,其中TABLEANAMEID = NAMEID? Then I want to get all the BVALUES by getting all the TABLEBNAMEIDs that correspond to the TABLEANAMEIDs. 然后,我想通过获取与TABLEANAMEID对应的所有TABLEBNAMEID来获取所有BVALUES。

I then want those BVALUES in a drop-down list on a seperate HTML page. 然后,我希望这些BVALUES在单独的HTML页面上的下拉列表中。 After a bit of Googling the solution I think would be to do some sort of a loop putting the BVALUES into a variable as all the NAMEIDs from TABLE B increment where the variable would be in an $BVALUE loop and the list values would show with all the BVALUES. 经过一番谷歌搜索的解决方案后,我认为将进行某种循环,将BVALUES放入变量中,因为表B中的所有NAMEID都会递增,其中变量将在$ BVALUE循环中,并且列表值将与所有变量一起显示BVALUES。

I hope I explained that right. 我希望我能解释正确。 I think I know what I'm trying to do but I have no idea how to actually implement it. 我想我知道我要做什么,但我不知道如何实际实施。 Please help guys. 请帮助大家。

You need to join those table together. 您需要将这些表连接在一起。 What you are describing is an m:n relation. 您要描述的是一个m:n关系。 In this case you have do use 2 joins like this: 在这种情况下,您必须使用2个这样的联接:

SELECT * FROM TableA AS a WHERE a.avalue = $AVALUE
JOIN TableAB AS a2b ON a.namevalue = a2b.id_a
JOIN TableB AS b ON a2b.id_b = b.id

Maybe u means, u want to get all BVALUE which has relation with selected AVALUE in table AB 也许您的意思是,您想获取与表AB中的所选AVALUE相关的所有BVALUE

$sql = "SELECT B.NAMEID as id, BVALUE as value FROM TABLEA A
        LEFT JOIN AB ON TABLEANAMEID=A.NAMEID
        LEFT JOIN TABLEB B ON TABLEBNAMEID=B.NAMEID
        WHERE AVALUE = $AVALUE";`

get mysql result 获取mysql结果

$result = mysql_query($sql);

iterate 重复

echo "<select>";
foreach ($r = mysql_fetch_object($result)) {
   echo '<option value="'.$r->id.'">'.$r->value.'</option>';
}
echo "</select>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM