Thank you for reading this, I am building a shopping cart with PHP session, when I click the submit button on the form, the $_POST was able to pass the value to the $_SESSION[''] array, but when I click the submit button again, the $_SESSION array only contain my last $_POST variable and the previous variables are all gone. As a shopping cart, the session array suppose to contain every data obtain from the POST value.
I checked with the SESSION_ID, which was able to shows the same ID when I submit the form, the var_dump shows the current SESSION array works (except only showing the last item). Please help me what is wrong in my code in order to keep the value into the SESSION array, thank you.
here is the full code, the sqli_query was replaced to specify item_id for troubleshooting, also this php will be included in another php that have an id in url, which seems irrelevant to this matter, but just for your information.
<?php if(!isset($_SESSION)){session_start();}?>
<?php
//if(isset($_GET['id']) && !empty($_GET['id'])){
require 'connecttosql.php';
$result = mysqli_query($con,"select COLOUR_EN, COLOUR_ZH, SIZE FROM mydb.item join mydb.colour using(item_id) join mydb.size using(item_id) WHERE ITEM_ID='T76'")
or die("Error: " . mysqli_error($con));
while($row = mysqli_fetch_array($result)){
$size[] = $row['SIZE'];
$colour_zh[] = $row['COLOUR_ZH'];
$colour_en[] = $row['COLOUR_EN'];
}
mysqli_free_result($result);
mysqli_close($con);
for($x=0;$x<count($colour_zh);$x++){
$colour[$x] = $colour_zh[$x] . "/" . $colour_en[$x];
}
echo "<form action='' method='POST'>";
echo "<ul>";
echo "<li>size: </li>";
echo "<li><select name = 'size'>";
foreach(array_unique($size) as $sizeli){
echo "<option value = '" . $sizeli . "'>" . $sizeli . "</option>";
}
echo "</select></li>";
echo "<li>colour: </li>";
echo "<li><select name = 'colour'>";
foreach(array_unique($colour) as $COLOURli){
echo "<option value = '" . $COLOURli . "'>" . $COLOURli . "</option>";
}
echo "</select></li>";
echo "<li><input type='SUBMIT' name='submit' value='ADDTOCART'></li>";
echo "</ul>";
$_SESSION['size'] = array();
$_SESSION['colour'] = array();
if(isset($_POST['submit'])) {
$_SESSION['size'][] = $_POST['size'];
$_SESSION['colour'][] = $_POST['colour'];
// $_SESSION['id'] = $_GET['id'];
}
echo SESSION_ID();
var_dump($_SESSION['size']);
var_dump($_SESSION['colour']);
// var_dump($_SESSION['id']);
/*
}else{
include 'index.php';
die();
}
*/
?>