Jump to content

Combining Session details help


squishypentagon

Recommended Posts

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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."'"));
}
?>

Link to comment
Share on other sites

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."'"));
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.