我一直在尝试将4个表连接在一起,因此我可以从每个表中检索数据并显示在我的网页上的表格中。
我已经使用JOIN命令修改了但我无法显示任何内容。
我的表架构是:
fuelrecords
FR_ID (Auto increment)
VEH_LIST_REG
FR_WE
fuelrecords_die
FRD_ID (AUTO INCREMENT)
FR_DIE_L
FR_DIE_C
fuelrecords_ID (foreign ID from fuelrecords)
fuelrecords_pet
FRP_ID (AUTO INCREMENT)
FR_PET_L
FR_PET_C
fuelrecords_ID (foreign ID from fuelrecords)
fuelrecords_oil
FRO_ID (AUTO INCREMENT)
FR_OIL_L
FR_OIL_C
fuelrecords_ID (foreign ID from fuelrecords)
我在页面上的代码是:
<?php
$qf=$_GET["q"];
$dtrf=trim($_GET["q"]);
list($d, $m, $y) = explode('-', $dtrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$q=strftime('%Y-%m-%d',$mk);
//$q=$_GET["q"];
$con = mysql_connect( 'IP', 'username', 'password' );
if( !$con ) {
die( 'Could not connect: ' . mysql_error() );
} else {
mysql_select_db( 'jbsrint', $con );
$sql="SELECT fuelrecords.VEH_LIST_REG,
fuelrecords.FR_WE,
fuelrecords_die.FR_DIE_L,
fuelrecords_die.FR_DIE_C,
fuelrecords_pet.FR_PET_L,
fuelrecords_pet.FR_PET_C,
fuelrecords_oil.FR_OIL_L,
fuelrecords_oil.FR_OIL_C
FROM fuelrecords
JOIN fuelrecords_die ON fuelrecords.FR_ID=fuelrecords_die.fuelrecords_ID
JOIN fuelrecords_pet ON fuelrecords.FR_ID=fuelrecords_pet.fuelrecords_ID
JOIN fuelrecords_oil ON fuelrecords.FR_ID=fuelrecords_oil.fuelrecords_ID
WHERE fuelrecords.FR_WE= '".$q."'";
$result = mysql_query($sql);
echo "<H3><font size=\"4px\" color=\"FFFFFF\">Fuel Records Weekly Input</font></H3>
<table border='1'>
<form name=\"FR_REV\">
<tr>
<th>Week Ending</th>
<th>$qf</th>
<tr>
<th>Vehicle Reg</th>
<th colspan=\"2\">Diesel Data</th>
<th colspan=\"2\">Petrol Data</th>
<th colspan=\"2\">Oil Data</th>
</tr>
<tr>
<td></td>
<td>Litres Used</td>
<td>Total Cost</td>
<td>Litres Used</td>
<td>Total Cost</td>
<td>Litres Used</td>
<td>Total Cost</td>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>
<td><input type=\"text\" name=\"VEH_LIST_REG[]\" value=\"".$row["fuelrecords.VEH_LIST_REG"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_DIE_L[]\" value=\"".$row["FR_DIE_L"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_DIE_C[]\" value=\"".$row["FR_DIE_C"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_PET_L[]\" value=\"".$row["FR_PET_L"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_PET_C[]\" value=\"".$row["FR_PET_C"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_OIL_L[]\" value=\"".$row["FR_OIL_L"]."\" readonly=\"readonly\"></td>
<td><input type=\"text\" name=\"FR_OIL_C[]\" value=\"".$row["FR_OIL_C"]."\" readonly=\"readonly\"></td>
</tr>";
}
}
echo"<tr>
<td></td><td></td>
</tr>
</form>
</table";
mysql_close($con);
?>
道歉,如果这很简单,但我对联接的了解很少,我在网上找到的所有例子似乎都与加入2个表有关。
感激地接受任何帮助。
艾伦
答案 0 :(得分:1)
这是因为查询连接了所有4个表中的记录,如果找不到任何表,则忽略整行。尝试LEFT JOIN而不是JOIN。
例如:
$sql="SELECT fuelrecords.VEH_LIST_REG,
fuelrecords.FR_WE,
fuelrecords_die.FR_DIE_L,
fuelrecords_die.FR_DIE_C,
fuelrecords_pet.FR_PET_L,
fuelrecords_pet.FR_PET_C,
fuelrecords_oil.FR_OIL_L,
fuelrecords_oil.FR_OIL_C
FROM fuelrecords
JOIN fuelrecords_die ON fuelrecords.FR_ID=fuelrecords_die.fuelrecords_ID
JOIN fuelrecords_pet ON fuelrecords.FR_ID=fuelrecords_pet.fuelrecords_ID
JOIN fuelrecords_oil ON fuelrecords.FR_ID=fuelrecords_oil.fuelrecords_ID
WHERE fuelrecords.FR_WE= '".$q."'";