【发布时间】:2017-01-14 04:14:28
【问题描述】:
我在 SQL 中有 2 个表,第一个名为 cars
`cars` (`car_id` int(100) NOT NULL,`car_first_registration` text NOT NULL, `car_brand` int(11) NOT NULL, `car_profile_image` text NOT NULL, `car_cat` int(11) NOT NULL, `car_price` decimal(10,0) NOT NULL, `car_vin` char(20) NOT NULL, `car_mileage` char(20) NOT NULL, `car_seats` int(11) NOT NULL, `car_gearbox` text NOT NULL, `car_ext_color` char(30) NOT NULL, `car_int_color` char(20) NOT NULL, `car_desc` text NOT NULL, `car_stock` varchar(255) NOT NULL, `car_keywords` text NOT NULL, `car_visibility` tinyint(1) NOT NULL, `car_ref` char(8) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1
`car_brands` (`brand_id` int(100) NOT NULL,`brand_title` text NOT NULL) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=latin1;
在汽车表中,我可以插入包含许多适合列的信息的汽车,但我有一个问题,汽车表中的 car_brand 是作为值存储的,这个值取自 car_brands :preloaded Brands,所以当我插入一个我从 car_brands 中的品牌中选择汽车,而 car_brands 有 2 列 id 和 title ,但问题是当我想获取汽车时 car_brand 存储为一个等于 car_brands 中 ID 的数字 例如,我有一辆品牌为 BMW 的汽车,并在 car_brands 表中存储为 ID=4,因此它将在汽车表中的 car_brand 中存储为 4 的值。我想在 GET 方法上使用此值以将产品显示为文本 (BMW) NOT 4 这是我的汽车显示代码,请告诉我要更正什么,这样我就可以显示名称而不是 ID
注意:不要混淆 car_brands 和 car_brand , car_brands 是一个表,而 car_brand 是汽车表中的一列
真的非常感谢:):):)!!!
<?php
function getCars(){
if(!isset($_GET['car_categories'])){
if(!isset($_GET['car_brands'])){
global $con;
$car_visibility = isset($_POST['car_visibility']);
$get_pro = "select * from cars where car_visibility= true";
$run_pro = mysqli_query($con, $get_pro)
or die("Error: ".mysqli_error($con));
$i = 0;
while($row_pro=mysqli_fetch_array($run_pro)){
$i++;
$car_id = $row_pro['car_id'];
$car_first_registration = $row_pro['car_first_registration'];
$car_brand = $row_pro['car_brand'];
$car_profile_image = $row_pro['car_profile_image'];
$car_cat = $row_pro ['car_cat'] ;
$car_price = $row_pro['car_price'];
$car_vin = $row_pro['car_vin'];
$car_mileage = $row_pro['car_mileage'];
$car_seats = $row_pro['car_seats'];
$car_gearbox = $row_pro['car_gearbox'];
$car_ext_color = $row_pro['car_ext_color'];
$car_int_color = $row_pro['car_int_color'];
$car_desc = $row_pro['car_desc'];
$car_stock = $row_pro['car_stock'];
$car_keywords = $row_pro['car_keywords'];
$car_visibility = $row_pro['car_visibility'];
$car_ref = $row_pro['car_ref'];
$get_brands ="SELECT * FROM car_brands";
$fetch_brands = "SELECT cars.car_id , cars.car_first_registration , cars.car_brand, cars.car_profile_image, cars.car_cat , cars.car_price , cars.car_vin, cars.car_mileage , cars.car_seats , cars.car_gearbox , cars.car_ext_color, cars.car_int_color, cars.car_desc , cars.car_desc , cars.car_stock , cars.car_keywords , cars.car_visibility , cars.car_ref , car_brands.brand_id
FROM cars
INNER JOIN car_brands
ON cars.car_brand=car_brands.brand_id";
echo "<div class='single_product'>";
echo "<h1><a href='details.php?car_id=$car_id' id='product_title'>$car_brand . $car_cat . $car_first_registration</a></h1>";
echo "<img src='admin_area/Car Profiles/$car_profile_image' />
<h2>$ $car_price</h2>
</div>";
}
}
}
}
?>
<?php getCars();?>
【问题讨论】:
-
也许在长查询中将
car_brands.brand_id替换为car_brands.brand_title会起作用,前提是您执行查询并在某处显示结果。 -
神圣的连续句,蝙蝠侠!使用标点符号将使您的描述更具可读性。
-
您已经知道如何加入。为什么不简单地在第一个查询中使用连接并在单个查询中获取品牌名称?
-
如何使用第一个查询在单个查询中获取品牌名称?谢谢
-
使用我更新的答案
标签: php html mysql function web