Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idnoble
Forum Commoner
Posts: 28
Joined: Wed Mar 31, 2004 4:09 am

Help

Post by idnoble »

Hello

I have just created a script for a shopping cart and in it, I instructed PHP to query a table in my database and insert it the results into another table in the same database.

To enabe users to be able to buy more than 1 product at a time I decided to use PHP sessionid, and asked PHP to insert the session id into the table to be updated, but each time I run the script I always have a Mysql error message, apparently all other data are being inserted into the table but not the session id can someone please help me out my code is below. thank you.

Code: Select all

<?php
//Start a session
session_start();
//create short variable name
$var = $_GET['var'];
$PHPSESSID = $_SESSION['PHPSESSID']; 
//connect to database
$session = mysql_connect("localhost", "xplosivewe", "platinum")or die(mysql_error());
if (!$session)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
mysql_select_db("xplosiveweb_net_-_xplosive")or die(mysql_error());
if ($var == "ALP2004"){
$query ="select Title, Cat_No, Price
         from movies
		 where Cat_No = 'ALP2004'";
}

else if($var == "GLD2000"){
$query ="select Title, Cat_No, Price
         from movies
		 where Cat_No = 'GLD2000'";
}
		 
else if($var == "THK200"){
$query ="select Title, Cat_No, Price
         from movies
		 where Cat_No = 'THK2000'";
}

//fetch the info 
$res = mysql_query($query) or die(mysql_error()); 
if(mysql_num_rows($res)){ 
    $row = mysql_fetch_assoc($res); 
} else { 
   die('No rows found'); 
} 
//add info into cart 
$addtocart = "insert into cart (id, session_id, Title, Cat_No, Price, Date) 
Values ('', '['PHPSESSID']', '{$row['Title']}', '{$row['Cat_No']}', '{$row['Price']}', now())"; 
mysql_query($addtocart) or die(mysql_error()); 


//redirect to showcart page
header("Location: showcart.php");
exit;
?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Look carefully at your INSERT query - you're asking it to insert [, not a session ID because of the quote of ' characters.

Where are you getting PHPSESSID from?

Extract the session ID to a variable and insert:

Code: Select all

$id = session_id();

$addtocart = "insert into cart (id, session_id, Title, Cat_No, Price, Date)
Values ('', '$id', '{$row&#1111;'Title']}', '{$row&#1111;'Cat_No']}', '{$row&#1111;'Price']}', now())";
mysql_query($addtocart) or die(mysql_error());
Post Reply