简体   繁体   中英

PHP/mySQL Data not being displayed in table

I am having an issue with MySQL data not being displayed via PHP
Below is the code which the data should be outputted into:

THIS IS individual_item_page.php

  <?php
    if (isset($_GET['suburb']))
    {
         $_SESSION["dog_park"] = $_GET['suburb'];
    }
    elseif (isset($_GET['keyword']))
    {
         $_SESSION["dog_park"] = $_GET['keyword'];
    }
    else 
    {
        $_SESSION["dog_park"] = $_GET['rating'] ;
    }
    ?>
    <h1><?php echo $_SESSION["dog_park"]; ?></h1>
    <table border="1" cellspacing="5" cellpadding="5" width="100%">
        <thead>
            <tr>
                <th>Park Name</th>
                <th>Street</th>
                <th>Suburb</th>
                <th>Dog Park Area (m2)</th>
            </tr>
        </thead>
        <tbody>
        <?php

            $result = $conn->prepare("SELECT * FROM dog_parks.items where suburb = ?");
            $result->execute(array($_SESSION['dog_park']));
            for($i=0; $row = $result->fetch(); $i++){
        ?>
            <tr>
                <td><label><?php echo $row['Park_Name']; ?></label></td>
                <td><label><?php echo $row['Street']; ?></label></td>
                <td><label><?php echo $row['Suburb']; ?></label></td>
                <td><label><?php echo $row['Dog_Park_Area_(m2)']; ?></label></td>

            </tr>
            <?php } ?>
        </tbody>
    </table>    

Below is the output page after the code has been executed:
(No data) 视觉输出

A basic overview of how the page is meant to work is,
I have 3 search types via
keyword
suburb
rating

If I want to search for a dog park by suburb I would select a suburb from the drop down box. (Code at bottom of page)

A table would then display the dog parks in that suburb/area which I would then click on one of those parks displayed (Hyperlinked), which will take me to the page I am having issues with, 'individual_item_page.php'

Below is the code for the suburb search page which then has a hyperlink to the 'individual_item_page.php' where the issue is..

THIS IS SUBURB SEARCH PAGE

 <table class="center"> <!-- Creating a table with the class of 'center' -->

        <!-- DROP DOWN BOX -->
        <?php
        $SUBURB = $_POST['suburb'];
                $stmt = $conn->prepare("SELECT dog_park_name from items where suburb='$SUBURB'");
                $stmt->execute();
            for($i=0; $row = $stmt->fetch(); ){ 
                    $_SESSION["dog_park".$i] = $row[0];             
          ?>
         <!-- DISPLAY RESULTS -->
        <tr> <!-- Adding the first table row -->
       <th>Dog Park</th>    <!-- Adding the second table header -->
        </tr>
       <tr> <!-- Adding the second table row -->
        <td><a href="individual_item_page.php?suburb='<?php echo $row[$i] ?>' " ><?php echo $row[$i] ?></a></td>         <!-- Add the second cell on the second row -->
        </tr>
                <?php } 
                ?>  

        </table>

This issue has baffled me for many hours now, any help would be appreciated.

On individual_item_page.php , you have a couple of things going on:

  1. You test for the presence of the first 2 GET variables you may be setting $_SESSION["dog_park"] to, but you fail to test for the third GET variable, $_GET['rating'] .

  2. Your SQL statement on that page is searching for a suburb, assuming that $_SESSION["dog_park"] was set to a suburb, despite the fact it may be set to $_GET['keyword'] or $_GET['rating'] . Also, I'm not sure why you are binding $_SESSION['dog park'] as an array parameter in your $result->execute() statement.

  3. On the suburb search page you are searching the table items but on the individual search page you are searching dog_parks.items . Was this intentional?

Important Note: On your suburb search page, you use a prepared statement but manually add a user entered variable instead of binding it, which defeats the protection supplied by binding parameters, which is to prevent user-entered data from being directly added to SQL statements.

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