简体   繁体   English

PHP的MySQL的选择查询

[英]php mysql select query

i have multiple drop list box that contain data from the database so i want to select the second drop list based on the section of the first one 我有多个下拉列表框,其中包含来自数据库的数据,因此我想根据第一个下拉列表的部分选择第二个下拉列表
the first table have these three fields district_id(primary key),district_name, governorate_id(foreign key) second table has these three fields village_id(primary key), village_name, district_id(foreign key) 第一个表具有这三个字段district_id(主键),district_name,governorate_id(外键)第二个表具有这三个字段village_id(主键),village_name,district_id(外键)

so i want the user to select the district box first then based on the select district name that have an id i want that the second box display all the village name that have a village.district_id = district.district_id. 所以我希望用户首先选择具有一个ID的地区名称,然后再选择区域框,我希望第二个框显示所有具有village.district_id = district.district_id的村庄名称。 can anyone help me?? 谁能帮我?? i was selecting each table independent from the other one but i need it to be based on the first one 我正在选择彼此独立的每个表,但我需要它基于第一个表

chunk of the code 代码块

function districtQuery(){

$distData = mysql_query("SELECT * FROM districts");

  while($recorddist = mysql_fetch_array($distData)){

     echo'<option value="' . $recorddist['district_name'] .  '">' . $recorddist['district_name'] . '</option>';

  }


}
// function for select by village
function villageQuery(){

//$villageData = mysql_query("SELECT * FROM village");

  $villageData = mysql_query("SELECT village_name FROM village WHERE district_id = ('SELECT district_id FROM districts')") or die (mysql_error());

  while($recordvillage = mysql_fetch_array($villageData)){

     echo'<option value="' . $recordvillage['village_name'] .  '">' . $recordvillage['village_name'] . '</option>';

  }


}

You need to use a server side call and populate the 2nd dropdown based on the selectyion of the first. 您需要使用服务器端调用,并根据第一个下拉列表的选择填充第二个下拉列表。

php + populate drop down menu on the selection of another PHP +填充下拉菜单上的另一个选择

Here i described two types of selected query 在这里,我介绍了两种选择的查询

$query = "select * from table_name";

above one select the whole table 上面的一个选择整个表

$query = "select * from table_name where id = 1";

above one select the particular record only 以上一项仅选择特定记录

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

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