Php 如何通过一个下拉菜单从两个表中获取数据
我有两个数据库表,如下所示:Php 如何通过一个下拉菜单从两个表中获取数据,php,mysql,Php,Mysql,我有两个数据库表,如下所示: CREATE TABLE IF NOT EXISTS `banners` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `long_name` varchar(50) NOT NULL, `visible` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=ut
CREATE TABLE IF NOT EXISTS `banners` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`long_name` varchar(50) NOT NULL,
`visible` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
以及:
我使用下面的代码根据地区id提取位置列表,并创建一个下拉菜单:
$query = 'SELECT id, banner_id, location FROM locations WHERE district_id =' . $district_id;
$result = mysql_query($query, $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"';
if($row['id'] == $location) { echo ' selected';} ;
echo '>' . $row['location'] . '</option>';
}
echo '</select>';
您需要一个连接查询。请尝试以下操作:
$query = 'SELECT l.id, l.banner_id, l.location, b.name
FROM locations l
INNER JOIN banners b ON (l.banner_id = b.id)
WHERE district_id =' . $district_id;
现在,您的结果集中既有名称
又有位置
:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"';
if($row['id'] == $location) { echo ' selected';} ;
echo '>' . $row['name'] . ' ' . $row['location'] . '</option>';
}
while($row=mysql\u fetch\u array($result)){
回显'.$row['name'].'.$row['location'].';
}
编辑:旁注
- 扩展已弃用,您应该使用或
- 为了防止SQL注入,您需要执行输入筛选和/或使用参数化查询。对于您的案例,最快的方法是将
强制转换为整数。有关更多信息,请参阅$district\u id
$query = 'SELECT l.id, l.banner_id, l.location, b.name
FROM locations l
INNER JOIN banners b ON (l.banner_id = b.id)
WHERE district_id =' . $district_id;
现在,您的结果集中既有名称
又有位置
:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"';
if($row['id'] == $location) { echo ' selected';} ;
echo '>' . $row['name'] . ' ' . $row['location'] . '</option>';
}
while($row=mysql\u fetch\u array($result)){
回显'.$row['name'].'.$row['location'].';
}
编辑:旁注
- 扩展已弃用,您应该使用或
- 为了防止SQL注入,您需要执行输入筛选和/或使用参数化查询。对于您的案例,最快的方法是将
强制转换为整数。有关更多信息,请参阅$district\u id
选择横幅、位置。其中locations.banner\u id=banner.id使用联接以获取两个表的相应标题名 喜欢
选择横幅、位置。where locations.banner\u id=banner.id这里是mysql查询,它将执行所需的结果
$query = "select locations.id,
locations.banner_id,
locations.location,
banners.name,
banners.id
from locations, banners
WHERE banners.id=locations.banner_id
and district_id =" . $district_id;
下面是mysql查询,它将执行所需的结果
$query = "select locations.id,
locations.banner_id,
locations.location,
banners.name,
banners.id
from locations, banners
WHERE banners.id=locations.banner_id
and district_id =" . $district_id;
在您的查询中进行连接在您的查询中进行连接假设两个表都可以通过一个连接访问即使$district_id是由另一个下拉菜单生成的,该菜单限制用户选择预定值,我仍然需要使用参数化查询吗?限制用户使用html或javascript(=前端)根本不添加任何安全性。总是可以绕过前端验证。你不应该依赖他们。例如,通过使用Chrome的web开发者工具栏,攻击者可以轻松地修改浏览器上的html/javascript,以发送他想要的任何值。谢谢,mesutozer!可以。假设两个表都可以通过一个连接访问。即使$district_id是由另一个下拉菜单生成的,该下拉菜单限制用户选择预定值,我仍然需要使用参数化查询?将用户限制在html或javascript(=前端)中根本不会增加任何安全性。总是可以绕过前端验证。你不应该依赖他们。例如,通过使用Chrome的web开发者工具栏,攻击者可以轻松地修改浏览器上的html/javascript,以发送他想要的任何值。谢谢,mesutozer!可以。我在另一个场景中使用了这个。非常感谢。我在另一个场景中使用了这个。非常感谢。