Hi David thanks for the reply, it's your fault I'm in this state after doing your tutorial, I thought I could change the world....
I am so very nearly there.
If I specify a username in my query it is working for me, but if I try to get the session variable to sit in it's place in the qhery it doesn't. Here is the code generated in both instances:
Specified - username 'marcobraynio'
mysql_select_db($database_boutique_wines, $boutique_wines);
$query_getCart = "SELECT cart.product_id, cart.quantity, cart.session_id, product.price, product.winery, product.colour, product.grape, product.year FROM cart, product WHERE cart.session_id = 'marcobraynio' AND cart.product_id = product.product_id ";
$getCart = mysql_query($query_getCart, $boutique_wines) or die(mysql_error());
$row_getCart = mysql_fetch_assoc($getCart);
$totalRows_getCart = mysql_num_rows($getCart);?>
Pulling in the Session Variable in the place of 'marcobraynio' I get this which doesn't work. I have started the session at the top of the page.
$colname_getCart = "-1";
if (isset($_SESSION['session_id'])) {
$colname_getCart = $_SESSION['session_id'];
}
mysql_select_db($database_boutique_wines, $boutique_wines);
$query_getCart = sprintf("SELECT cart.product_id, cart.quantity, cart.session_id, product.price, product.winery, product.colour, product.grape, product.year FROM cart, product WHERE cart.session_id = %s AND cart.product_id = product.product_id ", GetSQLValueString($colname_getCart, "text"));
$getCart = mysql_query($query_getCart, $boutique_wines) or die(mysql_error());
$row_getCart = mysql_fetch_assoc($getCart);
$totalRows_getCart = mysql_num_rows($getCart);
?>
I don't understand the code generated here - DW has placed a %s where I thought $_SESSION['session_id'] would go.
Cheers
Mark
marcobraynio wrote: I don't understand the code generated here - DW has placed a %s where I thought $_SESSION['session_id'] would go. |
The code that Dreamweaver has generated is correct. Dreamweaver uses the sprintf() function to insert the variable into the SQL query. Getting your head around sprintf() can be difficult to begin with. The function takes a minimum of two arguments. The first argument is the string that you want to insert variables into. Instead of inserting the variables directly in the string, you use placeholders that always begin with %. In this case %s represents a string.
The remaining argument(s) are the values you want to insert in place of the placeholders. If you look at the script generated by Dreamweaver, the second argument to sprintf() is GetSQLValueString($colname_getCart, "text"). GetSQLValueString() is a custom function created by Dreamweaver that checks the value of a variable, and makes it safe to insert in a SQL query. In your case, $colname_getCart is the equivalent of $_SESSION['session_id'].
There shouldn't be any problem using $_SESSION['session_id'] because PHP uses a different value to refer to the session ID (the internal one that identifies the session, not your variable). However, to avoid confusion, it would be better to use a different name, such as $_SESSION['user_id'].
If your query is working with a hard-coded name, but not with the session variable, it means that the session variable isn't being recognized on the page. Use echo to check whether the variable is being passed to the page.
if (isset($_SESSION['user_id'])) {
echo $_SESSION['user_id'];
} else {
echo 'Session variable does not exist';
}