-1

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

Mike V.
  • 37
  • 1
  • 6
  • Can you describe the problem more clearly please, preferably with some sample data? We don't know what you are saying is being changed – ADyson Feb 01 '23 at 09:04
  • i mean that it output this Array ( [vareNummer] => 182162 [vareNavn] => Solsikke [varePris] => 120 [vareBillede] => 63c7cba6cac6d7.24544415.jpg [vareAntal] => 1 ) – Benjamin Smith Feb 01 '23 at 09:08
  • and next time i run it it changing it instead of making a new – Benjamin Smith Feb 01 '23 at 09:09
  • Do you want to add the vareInfo array to the same session or in another session variable? – Webdeveloper_Jelle Feb 01 '23 at 09:10
  • what is it you are trying to store? if it is the cart you need to define an object or an array. (like `$_SESSION["cart"] = array(0 =>$vareInfo)` ) as you are doing it, if you already have a session variable with the key that matches the value of `$pris` will be overwritten. (`$pris = 1; $SESSION[1] = "abekat"; $_SESSION[$pris] ) "hukabuka"; echo $_SESSION[1]; //returns 1``) – JoSSte Feb 01 '23 at 09:11
  • i try to store it in a new session variable – Benjamin Smith Feb 01 '23 at 09:12
  • i know, but i am using the number, and the number is special for every item – Benjamin Smith Feb 01 '23 at 09:13
  • Is `$num` changing each time you run it? If not, then it will just overwrite your array in the Session instead of making a new one. From the behaviour you are describing, it sounds like `$num` must be the same each time. – ADyson Feb 01 '23 at 09:14
  • If your `$_POST['nummer']` value is the same, it gets overwritten, it should be different at all times, to prevent overwrite you should check if the session already exists – Webdeveloper_Jelle Feb 01 '23 at 09:14
  • the $num is the uniqid, for the items on my website – Benjamin Smith Feb 01 '23 at 09:16
  • Ok. But have you actually tested it? If the the $_SESSION value keeps being overwritten, then it must be because $num is being used repeatedly when you are posting your data. Log the content of `$_POST['nummer']` when you are testing your script – ADyson Feb 01 '23 at 09:17
  • To explain my point, here's a simulation of what would happen if $num is different each time you run the script: https://3v4l.org/6Rubc – ADyson Feb 01 '23 at 09:22
  • And now here's what would happen if $num is the same each time: https://3v4l.org/P7uEK – ADyson Feb 01 '23 at 09:23
  • I think it is about using the numbers for session variable names. See this post: [can a php $_SESSION variable have numeric id thus : $_SESSION](https://stackoverflow.com/questions/7450371/can-a-php-session-variable-have-numeric-id-thus-session1234) _and_ [dynamically name session isset how to use?](https://stackoverflow.com/questions/38482069/dynamically-name-session-isset-how-to-use). The solution would be something like from this post: https://stackoverflow.com/questions/18797251/notice-unknown-skipping-numeric-key-1-in-unknown-on-line-0 – prasad_ Feb 01 '23 at 10:38
  • @prasad_ those questions are all really old...have you checked if it is still the case? Because OP is implying that the data is being stored no problem, it's only the overwriting which is the problem. Also, despite the variable name $num, we actually have no proof that they are purely numeric values...in fact earlier in these comments it was referred to as containing a uniqid, which is alphanumeric. – ADyson Feb 01 '23 at 10:44
  • Actually they work in this case. I tried. Especially, the usage of `$_SESSION['my_data'][$num] = $vareInfo;`. This persists session data for each of the unique `$num`. @ADyson – prasad_ Feb 01 '23 at 10:47

3 Answers3

0

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";
}
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55
0

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 
        ) 
)
prasad_
  • 12,755
  • 2
  • 24
  • 36
-3

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";
}
?>
Code Genie
  • 21
  • 7
  • 1
    OP isn't asking for it to be read-only, they're asking for it not to keep overwriting the same index, and create a new one instead. Read the question again. – ADyson Feb 01 '23 at 09:18