简体   繁体   中英

How do I query two tables for a filtered result in mysql

I have a table with buildings(8052monticello) and a separate table with offices(individualspecs), Upon clicking on a respective building link in the buildings page I send the building id to through a get request to the handling page unit_specs.php, however unit_specs is showing me all the units in the table, however I only want unit_specs.php to display the units corresponding to its respective building. How can I fix this?

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

units table

buildings table

here is the full code:

 <!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>

Couple of things here...

  1. You aren't using anything from 8052monticello so there's no reason to join it in
  2. Assuming your request looks something like /unit_specs.php?id=123 , you need to compare individualspecs.fk_Id (your building ID) with $_GET['id'] . The best way to do so is use aprepared statement and bind the parameter.

For example

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

Useful links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM