deluxenathan deluxenathan - 6 months ago 8
SQL Question

PHP - echo $row produces same output

I am having an issue where the output of the statement below shows the correct content, but when I click one of the items (a hyperlink) they all end up at the same page with the same name of the bottom row:

<table class="center">
<!-- Creating a table with the class of 'center' -->
<!-- SEARCH FORM -->
<?php
$KEYWORD = $_POST['keyword'];
$stmt = $conn->prepare("SELECT DISTINCT dog_park_name FROM dog_parks.items WHERE dog_park_name LIKE '%{$KEYWORD}%'");
$stmt->execute();
for ($i = 0; $row = $stmt->fetch(); ++$i) {
$_SESSION["dog_park"] = "$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" ><?php echo $row[0]; ?></a></td> <!-- Add the second cell on the second row -->
</tr>
<?php
}
?>
</table>


I search a keyword and it produces the following output which is correct:

TRAMWAY ST DOG OFF LEASH AREA
STRINGYBARK DVE DOG OFF LEASH AREA


The problem comes in when I click on any of the results returned they all end up at the same page with the same name of the bottom row..

STRINGYBARK DVE DOG OFF LEASH AREA
STRINGYBARK DVE DOG OFF LEASH AREA


I think it was something to do with the
$stmt->fetch()


can't post pictures sorry...

Answer
Note: the change is here --> $row[0]  not  "$row[0]"

before $_SESSION["dog_park"] = "$row[0]";

after $_SESSION["dog_park".$i] = $row[0];

then get session value by

 $_SESSION["dog_park0"]; 
    $_SESSION["dog_park1"]; 
     $_SESSION["dog_park2"]; 
     $_SESSION["dog_park3"]; 
etc.,

Comments