Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2012, 08:17 PM   PM User | #1
nepoverljiv
New Coder

 
Join Date: Mar 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
nepoverljiv is an unknown quantity at this point
Question PHP session shopping cart problem

I have one code which I use to add articles in shopping cart. In another website working without any kind of problem, but on new one he makes me problem, he add me double article in shopping cart. I press once “add to basket” but he put me twice that articles. When I press “add to basket” I calling this code to found out which articles is:

PHP Code:
<?php
    session_start
();   
    
$var=$_SERVER['REQUEST_URI']; //reading 
    
$id substr(strrchr($var"korpa?"),);//reading first element after  "?"
    
$tbl_name="description"//table name

    
$_SESSION['korpa']+=$row[price];      

    if(
$_SESSION['numproduct']==0){
        
$_SESSION['articles']=array($id);
        
$_SESSION[numproduct]++;
    }else{
        
array_push($_SESSION['articles'],$id);
        
$_SESSION[numproduct]++;
    }

    
redirect("javascript: history.go(-1)");
?>
On main page I using this code to see what exist in shopping cart:

PHP Code:
$i=0;
$tbl_name="description";
$query count($_SESSION['articles']);

if(
$query==0){
    echo 
"Your shopping cart is empty.";
}else{
    echo 
"In your shopping cart exist:<br/><br/>";
    for(
$i=0;$i<=$query-1;$i++){
    
$br=$i+1;

connect();

    
$element=$_SESSION['articles'][$i];
    
$sql="SELECT * FROM $tbl_name WHERE id=$element";
    
$subject_set=mysql_query($sql);
    
$subject=mysql_fetch_array($subject_set);

    echo 
"   ".$br.". ";
    echo 
$subject['name'];
    echo 
" - <b>".$subject['prices']." USD</b><br/>  "
}

echo 
"<p align=right><a href=index.php?=order class=basket>Order</a> &middot; <a href=includes/delete.php>Delete items</a></p>"
Does anyone knows why articles goes twice in shopping cart?
nepoverljiv is offline   Reply With Quote
Old 12-22-2012, 09:53 AM   PM User | #2
nepoverljiv
New Coder

 
Join Date: Mar 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
nepoverljiv is an unknown quantity at this point
I have figured it out that problem is in this line of code:

PHP Code:
   $sql="SELECT * FROM $tbl_name WHERE id=$element";
    
$subject_set=mysql_query($sql);
    
$subject=mysql_fetch_array($subject_set); 
Everything before is ok, but here give me double values. Please help me
nepoverljiv is offline   Reply With Quote
Old 12-26-2012, 12:48 PM   PM User | #3
nepoverljiv
New Coder

 
Join Date: Mar 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
nepoverljiv is an unknown quantity at this point
I can't believe, no one to even try.
nepoverljiv is offline   Reply With Quote
Old 12-27-2012, 01:22 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by nepoverljiv View Post
I can't believe, no one to even try.
Returning to the previous page
Code:
redirect("javascript: history.go(-1)");
will cause a duplicate form-submission.

Session variables are initially strings, so it should really be:
PHP Code:
if($_SESSION['numproduct']=='0'){ 
assuming that the numproduct's are cleared (unset) at some stage.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-27-2012 at 01:25 AM..
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.