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.
 
     
    