Reputation: 581
Iam no expert and just a student and I am attempting to creating a shopping cart, so far i understand that i have to use sessions to store id. my way to connect to my database is always like this:
<html>
<head>Shopping cart</head>
<?php
$session_start();
?>
<body>
<?php
$serverName = "ephesus.cs.cf.ac.uk";
$dbName = "c1429814";
$user = "c1429814";
$pass = "fakepassword";
$con = mysqli_connect($serverName, $user, $pass, $dbName);
if(!$con){
die("failure to connect to the server ".mysqli_connect_error());
}
echo "<h1> Shopping cart </h1><br/>";
echo "<div class='text_border'>";
if(isset($_GET['id']))
$id=$_GET['id'];
else
$id=1;
if(isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch($action)
{
case "add":
if (isset($_SESSION['cart'][$id]))
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case "remove":
if (isset($_SESSION['cart'][$id]))
{
$_SESSION['cart'][$id]--;
if($_SESSION['cart'][$id]==0)
unset($_SESSION['cast'][$id]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
/*Display cart */
if (isset($_SESSION['cart']))
{
echo "<table border = 0 cellspacing=0 width='500'>";
$total = 0;
foreach($_SESSION['cart'] as $id => $x)
{
$query = "SELECT * FROM Software WHERE id = '$product' ";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$name = $row['name'];
$name=substr($name, 0, 40);
$price = $row['price'];
$the_cost= $price * $x;
$total = $total+$the_cost;
echo "<tr>";
echo "<td align='left'>$name </td>";
echo "<td align='right'>$x <a href='cart.php>id.".$id.">action=remove'>reduce'</a></td>";
echo "<td align='right'>= $the_cost";
echo "</tr>";
}
echo "<tr>";
echo "<td align='right'><br>Total=</td>";
echo "<td align='right'><b><br> $total </b></td>";
echo "</tr>";
echo "</table>";
}
else
echo "Cart is empty";
}
?>
</body>
</html>
I am getting an error which is called Undefined variable session, im not really sure what kind of variable i need to define to get around this? or is the way i connect to the database? any information that would could give me sufficient help and tips would be greatly appreciated. :)
Upvotes: 1
Views: 599
Reputation: 764
Please use the following code which is simplified your code. '$' should not use in your session_start();
<?php
session_start();
?>
<head>Shopping cart</head>
<body>
<?php
$serverName = "ephesus.cs.cf.ac.uk";
$dbName = "c1429814";
$user = "c1429814";
$pass = "ugsok4";
$con = mysqli_connect($serverName, $user, $pass, $dbName);
if (!$con) {
die("failure to connect to the server " . mysqli_connect_error());
}
echo "<h1> Shopping cart </h1><br/>";
echo "<div class='text_border'>";
$id = isset($_GET['id']) ? $_GET['id'] : 1;
$action = isset($_GET['action']) ? $_GET['action'] : 'empty';
switch ($action) {
case "add":
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id] ++;
} else {
$_SESSION['cart'][$id] = 1;
}
break;
case "remove":
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id] --;
if ($_SESSION['cart'][$id] == 0)
unset($_SESSION['cast'][$id]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
/* Display cart */
if (isset($_SESSION['cart'])) {
echo "<table border = 0 cellspacing=0 width='500'>";
$total = 0;
foreach ($_SESSION['cart'] as $id => $x) {
$query = "SELECT * FROM Software WHERE id = '$product' ";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$name = $row['name'];
$name = substr($name, 0, 40);
$price = $row['price'];
$the_cost = $price * $x;
$total = $total + $the_cost;
echo "<tr>";
echo "<td align='left'>$name </td>";
echo "<td align='right'>$x <a href='cart.php>id." . $id . ">action=remove'>reduce'</a></td>";
echo "<td align='right'>= $the_cost";
echo "</tr>";
}
echo "<tr>";
echo "<td align='right'><br>Total=</td>";
echo "<td align='right'><b><br> $total </b></td>";
echo "</tr>";
echo "</table>";
} else {
echo "Cart is empty";
}
}
?>
</body>
Upvotes: 1
Reputation: 148
Try to open the session before the HTML and remove $
Like this:
<?php
session_start();
?>
<html>
<head>Shopping cart</head>
<body>
<?php
$serverName = "ephesus.cs.cf.ac.uk";
$dbName = "c1429814";
$user = "c1429814";
$pass = "ugsok4";
$con = mysqli_connect($serverName, $user, $pass, $dbName);
if(!$con){
die("failure to connect to the server ".mysqli_connect_error());
}
echo "<h1> Shopping cart </h1><br/>";
echo "<div class='text_border'>";
if(isset($_GET['id']))
$id=$_GET['id'];
else
$id=1;
if(isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch($action)
{
case "add":
if (isset($_SESSION['cart'][$id]))
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case "remove":
if (isset($_SESSION['cart'][$id]))
{
$_SESSION['cart'][$id]--;
if($_SESSION['cart'][$id]==0)
unset($_SESSION['cast'][$id]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
/*Display cart */
if (isset($_SESSION['cart']))
{
echo "<table border = 0 cellspacing=0 width='500'>";
$total = 0;
foreach($_SESSION['cart'] as $id => $x)
{
$query = "SELECT * FROM Software WHERE id = '$product' ";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$name = $row['name'];
$name=substr($name, 0, 40);
$price = $row['price'];
$the_cost= $price * $x;
$total = $total+$the_cost;
echo "<tr>";
echo "<td align='left'>$name </td>";
echo "<td align='right'>$x <a href='cart.php>id.".$id.">action=remove'>reduce'</a></td>";
echo "<td align='right'>= $the_cost";
echo "</tr>";
}
echo "<tr>";
echo "<td align='right'><br>Total=</td>";
echo "<td align='right'><b><br> $total </b></td>";
echo "</tr>";
echo "</table>";
}
else
echo "Cart is empty";
}
?>
</body>
</html>
Upvotes: 1
Reputation: 334
use session_start();
(without the dollar sign) instead, since you are calling a function not accessing a variable. And as a side note: "To use cookie-based sessions, session_start() must be called before outputing anything to the browser." More here: http://php.net/manual/en/function.session-start.php
Upvotes: 1
Reputation: 2102
$ is used for php variables... you do not need $ when you use functions, and open it above all code...
so here :
//...
session_start();
//...
will be better....
Upvotes: 1