gpt4 book ai didi

php - 使用 php 显示我的数据库中的相关记录

转载 作者:行者123 更新时间:2023-11-30 23:24:27 24 4
gpt4 key购买 nike

我想根据其他表中的外键显示与特定主键相关的记录。如何使用 php 在其他表中显示该主键的记录??

例如:

table1

primary key 1: plate#1
primary key 2: plate#2
primary key 3: plate#3

table2

primary key 1: destination|route|revenue|plate# 1
primary key 2: destination|route|revenue|plate# 3

table3

primary key 1: diesel price|number of liters|plate# 1

primary key 2: diesel price|number of liters|plate# 3

我已经创建了一个页面来显示 table1 中的所有数据。我想在 table1table2 中显示与 table1 中的数据相关的数据,当我创建数据库时,它们已经与每个数据建立了关系其他。我的问题只是显示与 table1 相关的记录。我只想显示 plate#1 的记录,另一个显示 plate#2 的记录,依此类推。

最佳答案

这是一个粗略的例子:

<?php
$truck_id = mysql_real_escape_string($_GET['truck_id']);



$sql_PK = "SELECT * FROM table1 WHERE id = '{$truck_id}'";
$res_PK = mysql_query($sql_PK);
$row_PK = mysql_fetch_assoc($res_PK);

$truck_plate = $row_PK['plate'];



$truck_plate = mysql_real_escape_string($truck_plate);



$sql = "SELECT table2.plate, table2.destination, table2.route, table3.plate, table3.diesel_price, table3.num_of_liters FROM table2, table3 WHERE table2.plate = table3.plate AND table2.plate = '{$truck_plate}'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($res);


// this will give you the details from the following

//TABLE2
// plate
// destination
// route

//TABLE3
// plate
// diesel_price
// num_of_liters


?>

有几件重要的事情需要记住。首先是养成在数据库中不带空格命名字段的习惯,并改用_。所以柴油价格是 diesel_price 等。其次是确保您保护自己免受任何注入(inject),就像我在这里使用 mysql_real_escape_string 显示的那样

所以当有人点击 truckdetails.php?plate=mrtchi 时,它会根据车牌号查询数据库:mrtchi

关于php - 使用 php 显示我的数据库中的相关记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000782/

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