0

I am creating the code to add products to a cart using PHP. The script below works, but the added product can be shown only after refreshing the page.

  if ( isset( $_GET[ 'add' ] ) ) {

    $query = query( "SELECT * FROM products WHERE id = " . escape_string( $_GET[ 'add' ] ) );
    confirm( $query );

    while ( $row = fetch_array( $query ) ) {
      if ( $row[ 'quantity' ] != $_SESSION[ 'product_' . $_GET[ 'add' ] ] ) {
        $_SESSION[ 'product_' . $_GET[ 'add' ] ] += 1;
        redirect( $url );

      } else {
        $session->message( "We only have " . $row[ 'quantity' ] . " " . $row[ 'title' ] . " available" );
        redirect( $url );
      }
    }
  }

This is the query function:

 function query( $sql ) {
    global $connection;

    return mysqli_query( $connection, $sql );
  }

This is the very simple redirect function:

 function redirect( $location ) {
    return header( 'Location:' . $location );
 }

I cannot understand why.

DevDonkey
  • 4,835
  • 2
  • 27
  • 41
Daniel
  • 25
  • 5
  • try this: redirect($url);exit; – Faiyaz Alam Feb 29 '16 at 12:40
  • We can't see what your redirect function does. – Mathijs Segers Feb 29 '16 at 12:43
  • We also don't know what the session variable is, is Confirm a native function? I havent seen that one before. It probably doesn't redirect properly, like suggested by the other commenter. It might be because the redirect function is faulty or output has occured before etc. – Mathijs Segers Feb 29 '16 at 12:44
  • We also don't know what your `query` method does. You're also not adding anything in this code - you're only `SELECT`ing. Is this ajax? – h2ooooooo Feb 29 '16 at 12:54
  • Hi, I added the missing functions in my post. – Daniel Feb 29 '16 at 13:02
  • Im assuming this isnt ajax, because why would you redirect from an ajax script.. Which means we need much more info in order to help you out. What you've shown appears very confused (why redirect inside a while loop on both true/false sides of an if statement?). We need the HTML to provide context, and some clearer explanation of exactly what you are expecting/seeing. – DevDonkey Feb 29 '16 at 13:19

1 Answers1

0

I think session is working but your browser shows a cached page and when you refresh it then it updates the page.

Rehan Haider
  • 893
  • 11
  • 26
  • Hi Shan, thanks for your answer. Do you have any idea how to fix this problem? – Daniel Feb 29 '16 at 13:03
  • You are welcome. Try [this](http://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site) – Rehan Haider Feb 29 '16 at 13:10
  • Hi Shan, thanks for the link. Unfortunately the problem has not been solved. – Daniel Feb 29 '16 at 13:33
  • Sorry for that, when response is received does it redirects the page? – Rehan Haider Feb 29 '16 at 13:46
  • Yes, it redirects the page. – Daniel Feb 29 '16 at 13:54
  • After redirect you never set session value, then how it can show correct value after refresh? Instead of redirect, print session value to make sure it is changing or not. – Rehan Haider Feb 29 '16 at 14:02
  • Thanks @Daniel for accepting my answer. Infact it is my 1st answer which is accepted as correct, Thanks again :) I would like suggest you to show solution of your problem if anyone else face this problem could solve it easily. – Rehan Haider Mar 01 '16 at 14:11