skip to Main Content

I try to make a session variable where the session variable name should be diffrent, and therefore i make the session variable name a variable name:

$nummer = $_POST['nummer'];
$num = $nummer; 

$vareInfo = array(
    "vareNummer" => "$nummer",
    "vareNavn" => "$vare",
    "varePris" => $pris, 
    "vareBillede" => $VarBillede, 
    "vareAntal" => $antal
);
$_SESSION[$num] = $vareInfo;
$_SESSION[$pris] = "hukabuka";

but i dosen’t work, i just changeching the the other sessioen to the new value?

it output this:

Array ( [vareNummer] => 182162 
        [vareNavn] => Solsikke 
        [varePris] => 120 
        [vareBillede] => 63c7cba6cac6d7.24544415.jpg 
        [vareAntal] => 1 
    )

and next time i run it it changing it instead of making a new

3

Answers


  1. Check the session with that variable, if already set with that index then do not over write the session value
    you can use either isset or !empty in php

    <?php 
    $nummer = $_POST['nummer'];
    $num = $nummer; 
    
    $vareInfo = array(
        "vareNummer" => "$nummer",
        "vareNavn" => "$vare",
        "varePris" => $pris, 
        "vareBillede" => $VarBillede, 
        "vareAntal" => $antal
    );
    if(!empty($_SSESSION[$num])) {
    $_SESSION[$num] = $vareInfo;
    } 
    if(!empty($_SESSION[$pris])) {
    $_SESSION[$pris] = "hukabuka";
    }
    ?>
    
    Login or Signup to reply.
  2. To prevent that your session gets overwritten add a check if the session already exists.
    That way it only gets created once for each unique $num

    $nummer = $_POST['nummer'];
    $num = $nummer; 
    
    $vareInfo = array(
        "vareNummer" => "$nummer",
        "vareNavn" => "$vare",
        "varePris" => $pris, 
        "vareBillede" => $VarBillede, 
        "vareAntal" => $antal
    );
    if(!isset($_SESSION[$num])){
      $_SESSION[$num] = $vareInfo;
    }
    if(!isset($_SESSION[$pris])){
      $_SESSION[$pris] = "hukabuka";
    }
    
    Login or Signup to reply.
  3. An example how this can be made to work (using PHP 8.2):

    my_form.html:

    <form action="do_stuff.php" method="POST">
        <input id="data1" name="data1" type="text">
        <input id="data2" name="data2" type="text">
        <input type="submit" value="Do">
    </form>
    

    do_stuff.php:

    <?php
    session_start();
    
    $nummer = $_POST['data1'];
    $num = $nummer; 
    $pris = $_POST['data2'];
    $vareInfo = array(
        "vareNummer" => "$nummer",
        "varePris" => $pris
    );
    $_SESSION['my_data'][$num] = $vareInfo;
    $_SESSION['my_data'][$pris] = "hukabuka";
    
    // or, the session variables can be set using a prefix:
    //$_SESSION['my_data_' . $num] = $vareInfo;
    
    ?>
    

    EDIT ADD:

    An example session data from posting twice from the form using two unique data1 values would be as shown below. The values are 1234 and 7890.

    Array ( 
        [my_data] => 
            Array ( 
             [1234] => Array ( [vareNummer] => 1234 [varePris] => 55 ) 
             [55] => hukabuka 
             [7890] => Array ( [vareNummer] => 7890 [varePris] => 99 ) 
             [99] => hukabuka 
            ) 
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search