0

As the title says,, I want to know because it does not recognize the $ sessionID variable within the class DB_Functions.

When I check the table, all fields except the variable sessionID stored.

<?php

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

class DB_Functions {
....
....
....

public function insertItemToCart($dataMovieToInsertCart) {
        $precio = $dataMovieToInsertCart['precioMovie'];
        $id =  $dataMovieToInsertCart['claveMovie'];

        $query = "INSERT INTO carrito (session_carrito,id_pelicula,precio_pelicula) VALUES('$sessionID','$id','$precio')";
        $result = mysql_query($query) or die (mysql_error());

        if ($result) {
            return true;
        } else {
            return false;
        }
    }

}
?>
SoldierCorp
  • 7,610
  • 16
  • 60
  • 100
  • 6
    read up on variable scope. http://php.net/manual/en/language.variables.scope.php –  Jun 06 '12 at 03:37

1 Answers1

2

two way fix:

1.) use global keyword inside the function to catch up with global variables which are not available inside function scope

2.) if its only the case of session_id, use session_id() function inside function.

eg for 1:

global $sessionID;
$id =  $dataMovieToInsertCart['claveMovie'];
$query = "INSERT INTO carrito (session_carrito,id_pelicula,precio_pelicula) VALUES('$sessionID','$id','$precio')";
KoolKabin
  • 17,157
  • 35
  • 107
  • 145
  • 1
    as $_COOKIE['PHPSESSID'] is already a global, why not just use it as such? http://www.php.net/manual/en/reserved.variables.cookies.php –  Jun 06 '12 at 03:54