如何在mysql中查询两个表的过滤结果

6qftjkof  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(278)

我有一个带建筑物的表(8052monticello)和一个带办公室的单独表(individualspecs),在建筑物页面中单击相应的建筑物链接后,我通过get请求将建筑物id发送到处理页面unit\u specs.php,但是unit\u 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>
l7wslrjt

l7wslrjt1#

这里有几件事。。。
你没有使用任何来自 8052monticello 所以没有理由加入
假设你的请求看起来像 /unit_specs.php?id=123 ,你需要比较一下 individualspecs.fk_Id (您的建筑id)与 $_GET['id'] . 最好的方法是使用准备好的语句并绑定参数。
例如

$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;

有用的链接:
php:准备好的语句
控制结构的替代语法 mysqli_prepare() mysqli_stmt_bind_param() mysqli_stmt_execute() mysqli_stmt_bind_result() mysqli_stmt_fetch()

相关问题