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