Antonio Chelala Antonio Chelala - 1 month ago 5x
HTML Question

Fetch data from a table to another

I have 2 tables in SQL first one named 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;

In cars table I can insert cars with many information that fits the columns, but i have one problem that car_brand in cars table is store as a value,this value is taken from car_brands :preloaded brands , and so when i insert a car i choose from the brands that are in car_brands, and car_brands has 2 columns id and title , but the problem is when i want to get the cars the car_brand is stored as a number that is equal to the ID in car_brands
For exemple I have a car who's brand is BMW and stored as ID=4 in car_brands table so it will be stored as a value of 4 in car_brand in cars table. I want to have this value on GET method to display the products as a text (BMW) NOT 4
That is my display code of cars please tell me what to correct so i can display the name not the ID

NB: don't confuse between car_brands and car_brand , car_brands is a table and car_brand is a column in cars table

Thanks a lot really:):):)!!!

function getCars(){


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


You already know how to join. So, make it simple and do all you want with one query.

Change the first query to this:

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

You don't need any query more.

Wherever you need to show the brand name of the car, you can use $row_pro['brand_title']. So, you can change this:

$car_brand = $row_pro['car_brand'];

to this:

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

By the way, I recommend you read about the relations and indexes to make a better DataBase structure.