每次我点击刷新i它会增加最后添加项目的数量 [英] Every time i hit refresh i it increases the quantity for last added item

查看:41
本文介绍了每次我点击刷新i它会增加最后添加项目的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i am learning to build a shopping cart, the problem is every time i hit refresh it automatically increases the quantity for the added item, and even i close the window and run the file again i see my cart there means it does not destroy the session, i will give you the example so you can understand because my english is not that good,

<b>Problem-1:</b>Please read the Example-1, Example-2 and Summary

<b>Example-1:</b> I added two tea in the cart, than i added 3 coffee in the cart, its work fine but i notice if i hit refresh now it increases the coffee quantity automatically, why its doing it and what the solution. <i>(note: This time I Added coffee last in the Cart)</i>

<b>Example-2:</b> In last example i added the tea first and coffee last, so it increases the coffee when i hit refresh, but in this example i reverse the system means first i added coffee first than i added tea in the end, if i hit refresh now it increase the quantity for tea, <i>(note: This time I Added Tea last in the Cart)</i>

<b>Summary:</b> When i hit refresh it increases the quantity of last added item in the cart, if i hit refresh 5 times it going to increase the quantity 5 times means it does not matter how many times i hit refresh each time it increases the quantity, i dont know why its doing it,

<b>Second Problem:</b> As i am a begenner so it might be silly probmlem, i am using session, as far i know when i close my window and run the application again cart should be empty, but its not empty the each and every items which i added last time are still there with the last added quantity

<b>Example:</b> Lets say i have two tea in my cart and three coffee, and one t-shirt, i close my window and run the application again, i can see my last cart still there, means i still have two tea, three coffee and one t-shirt in the cart, as far i know i should not have any thing in the cart because i closed window.

Note: I am using XAMPP
Here is my code




session_start();

if (isset($_GET['add']))
{
    echo  $_SESSION['cart_'.$_GET['add']]+='1';
}

$db=mysqli_connect ('localhost','root','','dummyphpmyadmin') or die (mysql_error());


function products (){
    $db=mysqli_connect ('localhost','root','','dummyphpmyadmin') or die (mysql_error());
    $query=  mysqli_query($db,"SELECT * FROM products");
    
    
    while ($x=  mysqli_fetch_array($query)){
        echo $x['name'].'<br />'.
            $x['description'].'<br />'.
            $x['price'].'<br />'.
            '<a href="cart.php?add='.$x['id'].'">Add</a>'.'<br />';
    }
}

function cart () 
{
    foreach ($_SESSION as $name => $value)
    {
        if (substr($name, 0 ,5)=='cart_') 
        {
            $db=mysqli_connect ('localhost','root','','dummyphpmyadmin') or die (mysql_error());
           $id = substr ($name , 5 , (strlen($name)-5)); 
        $get=  mysqli_query($db,"SELECT id, name, price FROM dummyphpmyadmin.products 
            WHERE id=$id");
           while ($get_row=mysqli_fetch_array($get))
           {
               $total = $get_row['price'] * $value;
               echo $get_row['name'] . 'X' .$value. ' @ '
                   .$get_row['price'] .' = ' . $total. '<br />';
           }
        }
    }
}
cart();
products()
?>

<pre lang="PHP">

推荐答案

_GET [' add']))
{
echo
_GET['add'])) { echo


_SESSION [' cart _'
_SESSION['cart_'.


_GET ['' add']] + = ' 1';
}
_GET['add']]+='1'; }


这篇关于每次我点击刷新i它会增加最后添加项目的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆