I have this loop where i'm adding items to a session array. like this:

$SQL = "SELECT * FROM tbl_product where id ='{$cartid}'";

    //execute the query
    $result = mysql_query($SQL);
    //add the resultant data into a session array
    while($row = mysql_fetch_assoc($result)){
        $_SESSION['cartItems'][] = $row;

    }

Somewhere in my HTML document, i'm displaying the retrieved items in a table:

 foreach($_SESSION['cartItems'] as $product){
           echo '</tr>';
           echo ' <td class="product">'.$product['name'].'</td>';
          echo'<td class="quantity"><input type="text" value='.$product['quantity'].' class="inputform"/></td>';
          echo '<td class="item_price">'.$product['price'].'</td>';
          echo '<td class="item_total">'.$product['user'].'</td>';

        echo '<td class="item_remove"><a href = "cart-page.php?delid='.$product['id'].'"  style="color:red">Remove Product</a></td>';//to delete an item
        echo '</tr>';
         }

The problem is when i reload the page, the last entry in the table row gets duplicated i.e an identical row is automatically added. If i reload a hundred times, hundred identical rows will be added. That is very strange to me. Any ideas why i'm having this error? thanks

There are many ways to solve this problem.
One of this is:
1) Add unique key on certain values inserted if there is any.Now the record cannot be inserted rather catch exception for duplicate entry and handle exceptions.
2) On insert redirect page to another page ,so it will avoid re-entry of data.
3) Use session to insert data.On insert add it to session and once added if same data is inserted than show error message.

Hi, thanks. But i do not understand how u mean. What insertion are you referring to? Do you mean adding to the array? Can you please give me an example. thanks.

Hi, thanks. But i do not understand how u mean. What insertion are you referring to? Do you mean adding to the array? Can you please give me an example. thanks.

He means when you insert the data into the database redirect to another page immediately to prevent duplicate data. Like IIM said there are multiple solutions. The way I would do this is :

Capture the session until the products cart is added to the database. After the products are added take the user to a "your cart has been updated" page and generate a new session id destroying the products in the old session. Do not destroy the entire session or you will log them out. On the cart updated page pull the results from the database for the user to view. Do not add these products back to the session. Only add products to this new session if the user continues adding products.

Rinse and repeat until shopping is done.

Member Avatar for diafol

Are you sure that this is the right way to do it. Session variables are all well and good, but they can be overused. Can't you just make a fresh 'select' from the db? Or are you saving the entire basket in session vars? If so, then this is a bit awkward for the user if the session times out - they lose all their contents. I've very limited experience of e-commerce setups, but shouldn't there be a basket table or something similar?

@Banderson @diafol i'm not inserting any data into the database. All cart data is placed in a SESSION array and emptied when the user logs out. The SQL statement above just retrieves information about the product on which 'ADDTOCART' was clicked and saves it in a SESSION array (ie. $_SESSION['cartItems'][]). I then loop through the SESSION array to display products added. But the problem is after i display the items from the array(no database) and refresh the page, the last item on the list is duplicated.

Check to see if and why the query is being executed on page refresh and be sure you are not setting any stray session data on accident outside the query. Also try to close the connection after the query.

closing the connection doesn't solve it. i don't know if it's a query re-execution issue either, because i tried the code below to prevent the query from re-executing but still the same output.

$checker = "";
//cartid is the id of the product on which ADDTOCART was clicked
if($checker!=$cartid){
    $SQL = "SELECT * FROM tbl_product where id ='$cartid'";

    //execute the query
    $result = mysql_query($SQL);
    mysql_close($bd);
    //add the resultant data into a session array


    while($row = mysql_fetch_assoc($result)){
        $_SESSION['cartItems'][] = $row;

    }
    $checker = $cartid;
}

Try to comment out the complete query and hard code session data in. If that fixes it then it is because your query is being executed everytime you reload the page, otherwise you are writing to the session somewhere else in your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.