2

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();
}
*/
?>
1
  • You are assigning two empty arrays to the session (id and colour). When the form is posted, the values are replaced with posted values. Commented Jun 21, 2014 at 13:51

3 Answers 3

2

You reinitialize (and therefore reset) the arrays at every request:

$_SESSION['size'] = array();
$_SESSION['colour'] = array();

Add a check like this:

if(!isset($_SESSION['size'])) {
    $_SESSION['size'] = array();
}
if(!isset($_SESSION['colour'])) {
    $_SESSION['colour'] = array();
}
Sign up to request clarification or add additional context in comments.

Comments

0

it looks like you are resetting your session variables before it gets to this line...

if(isset($_POST['submit'])) {

try checking for existance of these before resetting...

$_SESSION['size'] = array();
$_SESSION['colour'] = array();

Comments

0

While doing this:

$_SESSION['size'] = array();
$_SESSION['colour'] = array();

you are doing a reset to $_SESSION['size'] and $_SESSION['colour']. You can replace it with: if(empty($_SESSION['size'])) $_SESSION['size'] = array(); if(empty($_SESSION['colour'])) $_SESSION['colour'] = array();

or just delete these two entries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.