Recently viewed products

Hi
I am having trouble writing code for a RECENTLY VIEWED PROPERTY LISTING for a estate Agency website.
Please help

What exactly is the problem?

What i want is like in E BAY ,which shows you the recent items,products viewed by the user.

I understand. But you say you’re having problems with your code. So I immagine you’ve already written some code, but it isn’t doing what you want. Post the code, explain what exactly is giving you trouble, and surely someone here will be able to help you.

If you want the whole code written for you, you might want to take a look here.

I have created the database in phpadmin on local PC.

<?php
include ‘db.inc’;

session_start();
if(!$_SESSION[‘user_id’]){
$_SESSION[‘user_id’] = rand(1, 1000000);
mysql_query(‘INSERT INTO product_recent (user_id) VALUES (’.$_SESSION[‘user_id’].‘)’);
}

LINE 18 $productid = $_GET[‘user_id’];

$query = ‘SELECT * FROM product_recent WHERE user_id = ‘.$_SESSION[‘user_id’].’’;
$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_query(‘UPDATE product_recent SET product_1="’.$productid.‘“, product_2=”’.$row[‘product_1’].‘“, product_3=”’.$row[‘product_2’].‘" WHERE user_id="’.$_SESSION[‘user_id’].‘"’);
?>

Error message: Notice: Undefined index: user_id in C:\xampp\htdocs\Amana new\ estrecent.php on line 18

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Amana new\ estrecent.php on line 22

First error:
You want to use $_GET[‘user_id’]. Do you pass the variable user_id in the query string? That is, does the link to the script have ?user_id=value ?

Second error:
This indicates the query has finished with an error. Do an echo of $query and of mysql_error() in case of an error:


$query = 'SELECT * FROM product_recent WHERE user_id = '.$_SESSION['user_id'].'';
$result = mysql_query($query) or die("mysql error" . mysql_error() . "in query: $query");

Thanks
Changed code this is new error;
Notice: Undefined index: user_id in C:\xampp\htdocs\Amana new\ estrecent.php on line 18
mysql errorNo database selectedin query: SELECT * FROM product_recent WHERE user_id = 983918

No database selected

You didn’t select a database. Or more probably, something went wrong connecting to the database.
What does db.inc contain? Delete userid and password before posting that code here!

Hi Guido
Is there another way off doing this without database ?
Chris

No

Thank you
Just busy sorting out the db.inc file to work.Wiil come back i have some more questions.
How do i show the information on the page ?
Chris

My global.php file
<?php
$db = mysql_connect(‘localhost’, ‘root’, ‘chrisedit52’);

if (!$db) {
    echo "Unable to establish connection to database server";
    exit;
}

if (!mysql_select_db('amanatest', $db)) {
    echo "Unable to connect to database";
    exit;
}

?>
New error :Notice: Undefined index: user_id in C:\xampp\htdocs\Amana new\ estrecent.php on line 18

<?php
include ‘global.php’;

session_start();
if(!$_SESSION[‘user_id’]){
$_SESSION[‘user_id’] = rand(1, 1000000);
mysql_query(‘INSERT INTO product_recent (user_id) VALUES (’.$_SESSION[‘user_id’].‘)’);
}

$productid = $_GET[‘user_id’]; LINE 18
$query = ‘SELECT * FROM product_recent WHERE user_id = ‘.$_SESSION[‘user_id’].’’;
$result = mysql_query($query) or die(“mysql error” . mysql_error() . “in query: $query”);

$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_query(‘UPDATE product_recent SET product_1="’.$productid.‘“, product_2=”’.$row[‘product_1’].‘“, product_3=”’.$row[‘product_2’].‘" WHERE user_id="’.$_SESSION[‘user_id’].‘"’);
?>

Change $_GET[‘user_id’] to $_SESSION[‘user_id’]

$_GET is used where you’re trying to get something off the querystring. Everywhere else on te page, you’re working off the session user.

New error on line 19

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\Amana new\ estrecent.php on line 19

Post the corrected code please. And put it between

 tags (the end tag needs to be /php with [] around it).

Thanks
Code:


<?php
include 'global.php';
 
session_start();
if(!$_SESSION['user_id']){
$_SESSION['user_id'] = rand(1, 1000000);
mysql_query('INSERT INTO product_recent (user_id) VALUES ('.$_SESSION['user_id'].')');
}
$productid =$_SESSION['user_id']
 
$query = 'SELECT * FROM product_recent WHERE user_id = '.$_SESSION['user_id'].'';
$result = mysql_query($query) or die("mysql error" . mysql_error() . "in query: $query");
 
$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_query('UPDATE product_recent SET product_1="'.$productid.'", product_2="'.$row['product_1'].'", product_3="'.$row['product_2'].'" WHERE user_id="'.$_SESSION['user_id'].'"'); 

Do you mind if i proceed to morrow again,something came up
Regards
Chris


$result = mysql_query($query) or die("mysql error" . mysql_error() . "in query: $query");

$result = mysql_query($query);

I don’t know why you get that error, but there’s one mysql_query() too much here :wink:
If you need to see the mysql errors, keep the top one, otherwise keep the bottom one.

Do you mind if i proceed to morrow again,something came up

Of course not. You can come back when you want to. And if I’m not here, others will be, and I’m sure there will be someone to help you further.

Remove the .‘’ at the end of the query.

Hi
These are my database fields :
user_id int(11) auto_increment
product_1 varchar(100)
product_2 varchar(100)
product_3 varchar(100)
time datetime (not functioning as yet

I tested the page on live server,after rectifying errors .it uploads “user_id” data every time i reload page in browser,then i get this error:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/stilbaai/public_html/testrecent.php:9) in /home/stilbaai/public_html/testrecent.php on line 12

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/stilbaai/public_html/testrecent.php:9) in /home/stilbaai/public_html/testrecent.php on line 12
PRESENT CODE:
<?php
include ‘global.php’;

LINE 12 session_start();
if(!$_SESSION[‘user_id’]){
$_SESSION[‘user_id’] = rand(1, 1000000);
mysql_query(‘INSERT INTO product_recent (user_id) VALUES (’.$_SESSION[‘user_id’].‘)’);
}
$productid =$_SESSION[‘user_id’];

$query = 'SELECT * FROM product_recent WHERE user_id = '.$_SESSION[‘user_id’];
$result = mysql_query($query) or die(“mysql error” . mysql_error() . “in query: $query”);

$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_query(‘UPDATE product_recent SET product_1="’.$productid.‘“, product_2=”’.$row[‘product_1’].‘“, product_3=”’.$row[‘product_2’].‘" WHERE user_id="’.$_SESSION[‘user_id’].‘"’);
?>
I AM NOT fully understanding the code.At least the code uploads user_id to database.
These numbers uploaded 98873,221870
My ESTATE AGENT WEBSITE has different catogaries of houses (farms,plots,houses with images) each has it own page,must i add this code to each page with a seperate database for each.
The PRODUCT_1 , PRODUCT_2 fields must capture the product description ?

Thanks for your patients

Move session_start() to the top of the script (before the include).

I AM NOT fully understanding the code.

That is a big problem. If you don’t understand the code you’re writing, who does?

The PRODUCT_1 , PRODUCT_2 fields must capture the product description ?

How would I know? You’re the one that designed the database.
By looking at those column names, it looks like there might be some more database normalization needed (if you don’t know what that is, google for it :wink: ).