squishypentagon Posted February 20, 2011 Share Posted February 20, 2011 I need some help please. I have a project that has a login script that is required before moving to a shopping cart and each use a session. I have a login script that i know works separate of the cart and vice versa. The problem is that when i access the cart i get the session errors and it quits working. So i need help with getting the 2 combined so i can run the cart script while a user is logged in. Here is the code i have. This is the login script session: <? ob_start(); session_start();include_once"config.php"; if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){ header("Location: login.php"); }else{ $user_data = "".$_SESSION['username'].""; $fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE `username`='".$user_data."'")); } ?> This is the script session for the shopping cart: <?php require_once 'library/config.php'; require_once 'library/category-functions.php'; require_once 'library/product-functions.php'; require_once 'library/cart-functions.php'; $_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI']; $catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0; $pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0; require_once 'include/header.php'; ?> and any help is very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/ Share on other sites More sharing options...
denno020 Posted February 20, 2011 Share Posted February 20, 2011 you need to have session_start() as the first thing in your script. Try moving that line of code above ob_start(). Otherwise, what errors are you getting? If you specify the actual errors, then maybe we could help more.. Denno Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177147 Share on other sites More sharing options...
squishypentagon Posted February 20, 2011 Author Share Posted February 20, 2011 I did what you suggested and i get this error, which is not the same as before: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home1/nwacompu/public_html/squishypentagon/cosmotgo/Membership/membersarea.php on line 19 and if i don't have the shopping cart code added then it works just fine Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177156 Share on other sites More sharing options...
denno020 Posted February 20, 2011 Share Posted February 20, 2011 Where is the shopping cart code? Denno Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177159 Share on other sites More sharing options...
denno020 Posted February 20, 2011 Share Posted February 20, 2011 Sorry I just saw in your original post about the shopping cart code. Can I instead get membersarea.php code. Or at the very least, a chunk of code around line 19. Denno Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177163 Share on other sites More sharing options...
squishypentagon Posted February 20, 2011 Author Share Posted February 20, 2011 This is all of it, so count down to 19 <?php require_once 'library/config.php'; require_once 'library/category-functions.php'; require_once 'library/product-functions.php'; require_once 'library/cart-functions.php'; $_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI']; $catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0; $pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0; require_once 'include/header.php'; ob_start(); session_start();include_once"config.php"; if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){ header("Location: login.php"); }else{ $user_data = "".$_SESSION['username'].""; $fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE `username`='".$user_data."'")); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177164 Share on other sites More sharing options...
denno020 Posted February 20, 2011 Share Posted February 20, 2011 Again, session_start() must be the very first thing in your file.. Move it to just after '<?' This goes with any and every php file that you want to be able to access SESSION with. Denno Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177166 Share on other sites More sharing options...
squishypentagon Posted February 20, 2011 Author Share Posted February 20, 2011 Ok, its letting me be logged in and view the shopping cart, but i'm still getting this error: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home1/nwacompu/public_html/squishypentagon/cosmotgo/Membership/membersarea.php on line 21 which i'll try and post the exact code, the last time it took out spaces and so the line 19 had changed. <?php session_start();include_once"config.php"; require_once 'library/config.php'; require_once 'library/category-functions.php'; require_once 'library/product-functions.php'; require_once 'library/cart-functions.php'; $_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI']; $catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0; $pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0; require_once 'include/header.php'; ob_start(); if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){ header("Location: login.php"); }else{ $user_data = "".$_SESSION['username'].""; $fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE `username`='".$user_data."'")); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177167 Share on other sites More sharing options...
squishypentagon Posted February 20, 2011 Author Share Posted February 20, 2011 Well, it did it again, but its linking to this line of code: $fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE `username`='".$user_data."'"));} Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177168 Share on other sites More sharing options...
kenrbnsn Posted February 20, 2011 Share Posted February 20, 2011 You need to see why the mysql query is failing, change: <?php $user_data = "".$_SESSION['username'].""; $fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `members` WHERE `username`='".$user_data."'")); ?> to <?php $user_data = $_SESSION['username']; $q = "SELECT * FROM `members` WHERE `username`='$user_data'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); $fetch_users_data = mysql_fetch_object($rs); ?> This will tell you why the query is failing. Ken Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177169 Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2011 Share Posted February 20, 2011 There's no logic in there to see what the query is doing and report any errors if it craters. There's also no need for all the extra string concatenation you're using. Change the else{} block to this: } else { $user_data = $_SESSION['username']; $query = "SELECT * FROM `members` WHERE `username`='$user_data'"; if( !$result = mysql_query($query) ) { echo "<br>Query : $query<br>Failed with error: " . mysql_error() . '<br>'; } else { $fetch_users_data = mysql_fetch_object($result); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177172 Share on other sites More sharing options...
denno020 Posted February 20, 2011 Share Posted February 20, 2011 Well I've just been playing with it, and the only thing I can suggest is, have you got the connection to your database working correctly? I was getting the same error as you until I added the real connection to my testing database. No errors anymore.. So look at your connection to mysql script and make sure that's right. Correct database name, username and password.. Denno Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177174 Share on other sites More sharing options...
squishypentagon Posted February 20, 2011 Author Share Posted February 20, 2011 well, i am getting this error Query : SELECT * FROM `members` WHERE `username`='bayyari' Failed with error: Table 'nwacompu_cart.members' doesn't exist for some reason its looking for nwacompu_cart.members when it should be looking for nwacompu_cart AND nwacompu_members Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177176 Share on other sites More sharing options...
squishypentagon Posted February 20, 2011 Author Share Posted February 20, 2011 ok, well i figured it out on my own. all i did was add a table to the cart table for the membership and then deleted the old membership database, and now it works just fine. Thanks for all the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/228280-combining-session-details-help/#findComment-1177199 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.