Update quantity In PHP Session Based Shopping Cart












1















Hello and a happy new year to everyone!
I work with php sessions...I have a cart...I want to update the quantity via session...
Here is my update action within the main file



if(isset($_POST["update"]))
{
if ($_SESSION["item_id"] = $_GET["id"])
{
$_SESSION['quantity'] = $_POST['quantity'];
}
}


And here is the form within the foreach loop to display the checked product



  $total = 0;
foreach ($_SESSION['cart'] as $item) {
?>
<tr>
<td><?php echo $item['name']; ?></td>
<td>
<form method="post" action="index2.php?action=update&id=<?php echo
$item["item_id"]; ?>">

<input type="text" name="quantity" value="1" class="form-control" />
<input type="submit" name="update" style="margin-top:5px;" class="btn btn-success" value="update" />
</form>
<?php echo $_SESSION['quantity']; //here I try to dislpay the new quantity that the user sets
echo $item["item_id"]; ?></td>


Problem



It changes the quantity in all the products within the basket and not in the specific one I want.



Please forgive me because I am a new bye.










share|improve this question

























  • Ummm - that looks like you only have one single quantity value in the session ... and item_id is looking pretty scalar as well...?

    – CD001
    Jan 2 at 17:07













  • Yes the session takes value from previous add to basket form. All I want is to change the value that the session has!

    – user4214258
    Jan 2 at 17:15











  • Sessions are user based. You only have 1 quantity session variable so if you change it then anywhere that you use it will have that value for that user's browser session. You'd need a different variable for each quantity or perhaps and array with the values.

    – abney317
    Jan 2 at 17:35
















1















Hello and a happy new year to everyone!
I work with php sessions...I have a cart...I want to update the quantity via session...
Here is my update action within the main file



if(isset($_POST["update"]))
{
if ($_SESSION["item_id"] = $_GET["id"])
{
$_SESSION['quantity'] = $_POST['quantity'];
}
}


And here is the form within the foreach loop to display the checked product



  $total = 0;
foreach ($_SESSION['cart'] as $item) {
?>
<tr>
<td><?php echo $item['name']; ?></td>
<td>
<form method="post" action="index2.php?action=update&id=<?php echo
$item["item_id"]; ?>">

<input type="text" name="quantity" value="1" class="form-control" />
<input type="submit" name="update" style="margin-top:5px;" class="btn btn-success" value="update" />
</form>
<?php echo $_SESSION['quantity']; //here I try to dislpay the new quantity that the user sets
echo $item["item_id"]; ?></td>


Problem



It changes the quantity in all the products within the basket and not in the specific one I want.



Please forgive me because I am a new bye.










share|improve this question

























  • Ummm - that looks like you only have one single quantity value in the session ... and item_id is looking pretty scalar as well...?

    – CD001
    Jan 2 at 17:07













  • Yes the session takes value from previous add to basket form. All I want is to change the value that the session has!

    – user4214258
    Jan 2 at 17:15











  • Sessions are user based. You only have 1 quantity session variable so if you change it then anywhere that you use it will have that value for that user's browser session. You'd need a different variable for each quantity or perhaps and array with the values.

    – abney317
    Jan 2 at 17:35














1












1








1








Hello and a happy new year to everyone!
I work with php sessions...I have a cart...I want to update the quantity via session...
Here is my update action within the main file



if(isset($_POST["update"]))
{
if ($_SESSION["item_id"] = $_GET["id"])
{
$_SESSION['quantity'] = $_POST['quantity'];
}
}


And here is the form within the foreach loop to display the checked product



  $total = 0;
foreach ($_SESSION['cart'] as $item) {
?>
<tr>
<td><?php echo $item['name']; ?></td>
<td>
<form method="post" action="index2.php?action=update&id=<?php echo
$item["item_id"]; ?>">

<input type="text" name="quantity" value="1" class="form-control" />
<input type="submit" name="update" style="margin-top:5px;" class="btn btn-success" value="update" />
</form>
<?php echo $_SESSION['quantity']; //here I try to dislpay the new quantity that the user sets
echo $item["item_id"]; ?></td>


Problem



It changes the quantity in all the products within the basket and not in the specific one I want.



Please forgive me because I am a new bye.










share|improve this question
















Hello and a happy new year to everyone!
I work with php sessions...I have a cart...I want to update the quantity via session...
Here is my update action within the main file



if(isset($_POST["update"]))
{
if ($_SESSION["item_id"] = $_GET["id"])
{
$_SESSION['quantity'] = $_POST['quantity'];
}
}


And here is the form within the foreach loop to display the checked product



  $total = 0;
foreach ($_SESSION['cart'] as $item) {
?>
<tr>
<td><?php echo $item['name']; ?></td>
<td>
<form method="post" action="index2.php?action=update&id=<?php echo
$item["item_id"]; ?>">

<input type="text" name="quantity" value="1" class="form-control" />
<input type="submit" name="update" style="margin-top:5px;" class="btn btn-success" value="update" />
</form>
<?php echo $_SESSION['quantity']; //here I try to dislpay the new quantity that the user sets
echo $item["item_id"]; ?></td>


Problem



It changes the quantity in all the products within the basket and not in the specific one I want.



Please forgive me because I am a new bye.







php forms session






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 17:01









Funk Forty Niner

1




1










asked Jan 2 at 17:00







user4214258




















  • Ummm - that looks like you only have one single quantity value in the session ... and item_id is looking pretty scalar as well...?

    – CD001
    Jan 2 at 17:07













  • Yes the session takes value from previous add to basket form. All I want is to change the value that the session has!

    – user4214258
    Jan 2 at 17:15











  • Sessions are user based. You only have 1 quantity session variable so if you change it then anywhere that you use it will have that value for that user's browser session. You'd need a different variable for each quantity or perhaps and array with the values.

    – abney317
    Jan 2 at 17:35



















  • Ummm - that looks like you only have one single quantity value in the session ... and item_id is looking pretty scalar as well...?

    – CD001
    Jan 2 at 17:07













  • Yes the session takes value from previous add to basket form. All I want is to change the value that the session has!

    – user4214258
    Jan 2 at 17:15











  • Sessions are user based. You only have 1 quantity session variable so if you change it then anywhere that you use it will have that value for that user's browser session. You'd need a different variable for each quantity or perhaps and array with the values.

    – abney317
    Jan 2 at 17:35

















Ummm - that looks like you only have one single quantity value in the session ... and item_id is looking pretty scalar as well...?

– CD001
Jan 2 at 17:07







Ummm - that looks like you only have one single quantity value in the session ... and item_id is looking pretty scalar as well...?

– CD001
Jan 2 at 17:07















Yes the session takes value from previous add to basket form. All I want is to change the value that the session has!

– user4214258
Jan 2 at 17:15





Yes the session takes value from previous add to basket form. All I want is to change the value that the session has!

– user4214258
Jan 2 at 17:15













Sessions are user based. You only have 1 quantity session variable so if you change it then anywhere that you use it will have that value for that user's browser session. You'd need a different variable for each quantity or perhaps and array with the values.

– abney317
Jan 2 at 17:35





Sessions are user based. You only have 1 quantity session variable so if you change it then anywhere that you use it will have that value for that user's browser session. You'd need a different variable for each quantity or perhaps and array with the values.

– abney317
Jan 2 at 17:35












1 Answer
1






active

oldest

votes


















0














I added one more dimension to the $_SESSION array, using the item ID:



$_SESSION['item'][$_GET['id']]['quantity'] = $_POST['quantity'];


Then when updating the quantity in the form:



echo $_SESSION['item'][$item['item_id']]['quantity'];





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010283%2fupdate-quantity-in-php-session-based-shopping-cart%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I added one more dimension to the $_SESSION array, using the item ID:



    $_SESSION['item'][$_GET['id']]['quantity'] = $_POST['quantity'];


    Then when updating the quantity in the form:



    echo $_SESSION['item'][$item['item_id']]['quantity'];





    share|improve this answer




























      0














      I added one more dimension to the $_SESSION array, using the item ID:



      $_SESSION['item'][$_GET['id']]['quantity'] = $_POST['quantity'];


      Then when updating the quantity in the form:



      echo $_SESSION['item'][$item['item_id']]['quantity'];





      share|improve this answer


























        0












        0








        0







        I added one more dimension to the $_SESSION array, using the item ID:



        $_SESSION['item'][$_GET['id']]['quantity'] = $_POST['quantity'];


        Then when updating the quantity in the form:



        echo $_SESSION['item'][$item['item_id']]['quantity'];





        share|improve this answer













        I added one more dimension to the $_SESSION array, using the item ID:



        $_SESSION['item'][$_GET['id']]['quantity'] = $_POST['quantity'];


        Then when updating the quantity in the form:



        echo $_SESSION['item'][$item['item_id']]['quantity'];






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 5 at 20:19







        user4214258







































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010283%2fupdate-quantity-in-php-session-based-shopping-cart%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith