I’m trying to save the row $row['Id']
in a php session so I can use this variable on the next page to have it fill the $product_id
variable dynamically.
So far this is what I’ve got:
<?php $sql = "SELECT * FROM assortiment WHERE Categorie = '$productid' ORDER BY Id DESC ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
SESSION_START();
$identi = $row['Id'];
$_SESSION["product_id"] = $identi;
echo "<a href='/dbtest/dinner/amsterdam/".$row["Slug"]. "/" . $row["Id"] . "'><div class='products'><div class='col-sm-3'><div class='product-img'>";
echo "<img src='http://www.example.nl/Uploaded_files/Thumbs/" .$row['Fotomateriaal']. ".jpg'>";
echo "</div></div><div class='col-sm-6'>";
echo "<div class='h2-container'><h2>" . $row["Product"]. "</h2></div>" . $row["Samenvatting"]. "" . $_SESSION["product_id"] . "";
echo "</div><div class='col-sm-3'><div class='col-sm-12 text-right'>
<div class='border'>
<p style='margin-bottom:20px;width:30px;'><i class='fa fa-heart' aria-hidden='true'></i></p>
</div>
</div>";
echo "<table>
<tr>
<td><i class='fa fa-users' aria-hidden='true'></i></td>
<td><p>vanaf 10 personen</p></td>
</tr>
<tr>
<td><i class='fa fa-clock-o' aria-hidden='true'></i></td>
<td>" . $row["Tijdsduur"] . " uur</td>
</tr>
<tr>
<td><i class='fa fa-euro' aria-hidden='true'></i></td>
<td>vanaf " . $row["VerkoopPP40"] ." p.p. <small>excl btw</small></td>
</tr>
</table></div></div></a>";
}
} else {
echo "0 results";
}?>
As you can see I’m trying to save the $row['Id']
. First I make this ID in a $identi variable so that I can echo it. Then I save the ID in a $_session["product_id"]
and echo it later on in the HTML. This works. But when I click on the link to go to the next page it looks like the session isn’t saved.
I cannot echo the product_id
on the next page using:
<?php
if(isset($_SESSION['product_id'])){
echo $_SESSION['product_id'];
};
?>
I think it’s not being stored, or (probably) I’m doing this all wrong? I found something on stack-overflow that started the session inside another part of the database query:
<?php
$sql = "SELECT * FROM assortiment WHERE Subcategorie = '$product_id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$fotos = $row["Fotomateriaal"];
$productnaam = $row["Product"];
$contenttext = $row["WebTekst"];
$tijdsduur = $row["Tijdsduur"];
$prijs = $row["VerkoopPP40"];
$sub = $row["Subcategorie"];
$cate = $row["Categorie"];
$seo = $row["SEO_Pagetitle"];
$youtube = $row["Actief_Avontuur"];
$_SESSION["product_id"] = $row["Id"];
}
} else {
$value = "Geen resultaten";
} ?>
But this give me nothing also. Does anyone know why this isn’t working. Normally everything works fine if I want to store a variable in a session like normal (just using PHP and WordPress) but this just wont work. Any help would be much appreciated.
2
Answers
Actually you write
session_start();
in wrong way(SESSION_START();
) and wrong place (Insidewhile()
).On each php page add
session_start();
on top of each page just after starting<?php
Note:-
session_start();
needed to fetch+manipulateSESSION
dataThis will save just the last info in the row. You have to save the row in query.