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

标签 php html mysql function web

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

相关文章:

php - 如何修复 'SQLSTATE[42S22]: Column not found: 1054 Unknown column ' 中的 'where clause' id' (SQL : select * from `users` where `id` = 0 limit 1)'

html - 不知道为什么setAttribute()对音频元素不起作用

javascript - 上传到本地主机时代码看起来损坏了

php - JOIN 不检索行

php - PDO准备语句麻烦

php - 准确保存和显示文本的输入和提交方式

php - 使用 Laravel RouteNotifcationForMail() 发送到多个电子邮件地址?

mysql - Mysql 未初始化常量

php - 一段时间后使php session 变量过期

html - 强制媒体查询遵守决议