2

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Steggie
  • 525
  • 8
  • 31

2 Answers2

1

Actually you write session_start(); in wrong way( SESSION_START();) and wrong place (Inside while()).

On each php page add session_start(); on top of each page just after starting <?php

Note:- session_start(); needed to fetch+manipulate SESSION data

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Maybe I should make a new question but, do you perhaps know why it only stores that last items Id and not all the items separate? – Steggie Jan 13 '17 at 12:37
  • @Steggie because you are overriding your session variable again and again in while loop. So instead of this:- `$_SESSION["product_id"] = $identi;` write `$_SESSION["product_id"][] = $identi;` – Alive to die - Anant Jan 13 '17 at 13:15
  • I did the following: `$identi = $row['Id']; $_SESSION["product_id"]= array(); $_SESSION["product_id"][] = $identi; var_dump($_SESSION['product_id']);` The var_dump gives me this `array(1) { [0]=> string(5) "12611" } ` But the session just stores the word `array` nothing more... – Steggie Jan 13 '17 at 13:23
  • @Steggie ask another question with that much amount of code. (not full code of here). Hesr its not possible to see and tell the answer – Alive to die - Anant Jan 13 '17 at 13:28
  • please look at this, thanks: http://stackoverflow.com/questions/41635943/php-session-of-database-id-only-stores-the-word-array – Steggie Jan 13 '17 at 13:40
-1

This will save just the last info in the row. You have to save the row in query.

  • If you're using PDO, use PDO::lastInsertId. or $id = lastInsertId($connection) thats if you are using MYSQl as your DB. If you're using Mysqli, use mysqli::$insert_id. – Asuquo12 Aug 07 '21 at 21:23