gpt4 book ai didi

php - 从一个表中获取数据到另一个表

转载 作者:行者123 更新时间:2023-11-30 22:13:11 25 4
gpt4 key购买 nike

我在 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;

在 cars 表中,我可以插入包含许多适合列的信息的汽车,但我有一个问题,即 cars 表中的 car_brand 作为一个值存储,该值取自 car_brands :preloaded brands ,因此当我插入一个我从 car_brands 中的品牌中选择汽车,car_brands 有 2 列 id 和 title,但问题是当我想获得汽车时,car_brand 存储为等于 car_brands 中 ID 的数字例如,我有一辆品牌为 BMW 的汽车,在 car_brands 表中存储为 ID=4,因此它将在 cars 表中的 car_brand 中存储为值 4。我想在 GET 方法上使用此值以将产品显示为文本 (BMW) NOT 4那是我的汽车显示代码请告诉我更正什么以便我可以显示名称而不是 ID

注意:不要混淆 car_brands 和 car_brand,car_brands 是一个表而 car_brand 是 cars 表中的一列

真的非常感谢:):)!!!

<?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();?>

最佳答案

您已经知道如何加入。因此,让它变得简单,并通过一个查询完成您想要的一切。

将第一个查询更改为:

SELECT * FROM cars         
INNER JOIN car_brands
ON cars.car_brand=car_brands.brand_id AND cars.car_visibility = 1

您不再需要任何查询。

在任何需要显示汽车品牌名称的地方,都可以使用 $row_pro['brand_title']。所以,你可以改变这个:

$car_brand = $row_pro['car_brand'];

为此:

$car_brand_id = $row_pro['car_brand'];
$car_brand_name = $row_pro['brand_title'];

顺便说一句,我建议您阅读关系和索引,以构建更好的数据库结构。

关于php - 从一个表中获取数据到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39353502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com