将数据从表中提取到另一个表

时间:2016-09-06 16:13:02

标签: 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;

在汽车表中,我可以插入包含适合列的许多信息的汽车,但我有一个问题,汽车表中的car_brand存储为一个值,此值取自car_brands:预装品牌,所以当我插入一个汽车我从car_brands中的品牌中选择,car_brands有2列id和title,但问题是当我想要获取汽车时car_brand存储为一个等于car_brands中ID的数字 例如,我有一辆汽车,其品牌是宝马,并在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();?>

1 个答案:

答案 0 :(得分:0)

您已经知道如何加入。所以,简单一点,用一个查询做你想做的一切。

将第一个查询更改为:

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'];

顺便说一下,我建议你阅读有关关系和索引的内容,以便建立更好的DataBase结构。