我有一个包含建筑物的表(8052monticello)和一个单独的包含办公室的表(individualspecs),在建筑物页面中单击相应的建筑物链接后,我通过获取请求将建筑物ID发送到处理页面unit_specs.php ,但是unit_specs向我显示了表中的所有单位,但是我只希望unit_specs.php显示与其各自建筑物相对应的单位。我怎样才能解决这个问题?
$querystats='SELECT individualspecs.Space, individualspecs.Size,
individualspecs.Price, individualspecs.Id';
$querystats.= ' FROM individualspecs';
$querystats.= ' INNER JOIN 8052monticello ON
individualspecs.fk_Id=8052monticello.id';
if ($r = mysqli_query($connection, $querystats)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {
if ($row['Price']){
print "<p><h3> Unit # {$row['Space']}</h3>
{$row['Size']} Sq Feet<br>
{$row['Price']} Monthly rent<br>
<a href=\"edit_UNIT.php?Id={$row['Id']}\">Edit</a>
<a href=\"delete_UNIT.php?Id={$row['Id']}\">Delete</a>
</p><hr>\n";
}
}
}
这是完整的代码:
<!doctype html>
<html lang="en">
<head>
<style>
.container{
padding: 1em;
}
</style>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-
Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
<meta charset="utf-8">
<title>View the units @ </title>
</head>
<body>
<div class="container">
<h1>Units</h1><hr>
<?php // Script 12.6 - view_BUILDINGS.php
$connection = mysqli_connect('localhost', 'user', 'password',
'BUILDINGS');
// Define the query but only for info:
$querystats='SELECT individualspecs.Space, individualspecs.Size,
individualspecs.Price, individualspecs.Id';
$querystats.= ' FROM individualspecs';
$querystats.= ' WHERE individualspecs.fk_building= 8052monticello.UNIT';
if ($r = mysqli_query($connection, $querystats)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {
if ($row['Price']){
print "<p><h3> Unit # {$row['Space']}</h3>
{$row['Size']} Sq Feet<br>
{$row['Price']} Monthly rent<br>
<a href=\"edit_UNIT.php?Id={$row['Id']}\">Edit</a>
<a href=\"delete_UNIT.php?Id={$row['Id']}\">Delete</a>
</p><hr>\n";
}
}
}
//end of get image query
else { // Query didn't run.
print '<p style="color: red;">Could not retrieve the data because:<br>' .
mysqli_error($connection) . '.</p><p>The query being run was: ' .
$querystats . '</p>';
} // End of query IF.
mysqli_close($connection); // Close the connection.
?>
</div>
</body>
</html>
答案 0 :(得分:0)
这里的事物……
8052monticello
中的任何内容,因此没有理由在其中加入
/unit_specs.php?id=123
,则需要将individualspecs.fk_Id
(您的建筑物ID)与$_GET['id']
进行比较。最好的方法是使用prepared statement并绑定参数。例如
$buildingId = $_GET['id'] ?? null; // PHP 7 syntax, use "$buildingId = isset($_GET['id']) ? $_GET['id'] : null;" for older versions
$stmt = $connection->prepare(
'SELECT Space, Size, Price, Id FROM individualspecs WHERE Price > 0 AND fk_Id = ?');
$stmt->bind_param('i', $buildingId); // bind the first parameter as an integer
$stmt->execute();
// I just find bind_result() easier to work with, YMMV
$stmt->bind_result($space, $size, $price, $id);
// PHP's alternative syntax makes for better readability IMO
while ($stmt->fetch()) : ?>
<p>
<h3> Unit # <?= $space ?></h3> <!-- FYI: H3 should not be in a P tag -->
<?= $size ?> Sq Feet<br>
<?= $price ?> Monthly rent<br>
<a href="edit_UNIT.php?Id=<?= $id ?>">Edit</a>
<a href="delete_UNIT.php?Id=<?= $id ?>">Delete</a> <!-- deleting via a GET request is a bad idea -->
</p>
<hr>
<?php endwhile;
有用的链接: