假设您的 2 表结构如下:
VEHICLES
id
manufacturerId
vehicleName
MANUFACTURERS
id
manufacturerName
您可以通过像这样查询数据库来为用户创建您的汽车制造商选择菜单:
// query the database
$q = 'SELECT id, manufacturerName FROM manufacturers';
$r = mysqli_query($link, $q);
// display a select menu using id and manufacturerName
echo '<select name="manufacturer">';
while($row = mysqli_fetch_assoc($r)) {
echo '<option value="'.$row['id'].'">'.$row['manufacturerName'].'</option>';
}
echo '</select>';
要使用该菜单中的发布数据将车辆和制造商 ID 添加到您的车辆表中:
$q = "INSERT INTO vehicles (manufacturerId, vehicleName) VALUES ({$_POST['manufacturer']}, '{$_POST['vehicleName']}')";
mysqli_query($link, $q);
最后,如果您希望在同一查询中选择车辆名称和制造商,您可以像这样连接表:
// Select vehicle name and manufacturer for vehicle with id of 1
$q = "SELECT v.vehicleName, m.manufacturerName, v.id AS vehicleId, m.id AS manufacturerId
FROM vehicles AS v, manufacturers AS m
WHERE v.manufacturerID = m.id
AND v.id = 1";
mysqli_query($link, $q);
我认为这应该以一种或另一种方式回答你所有的问题!