Iam没有专家,只是一个学生,我试图创建一个购物车,目前为止我明白,我必须使用会话来存储身份证。我的方式连接到我的数据库总是这样的:未定义的变量:会话开始(购物车尝试)
<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>
我越来越被称为未定义的变量会话的错误,我不是很确定我需要定义来解决这个问题是什么样的变量?或者是我连接到数据库的方式?任何可能会给我足够的帮助和提示的信息将不胜感激。 :)
删除您$ ...只是在session_start(); – Julo0sS
omg,谢谢..哈哈。即时通讯这样的新手 – johnobc
标记答案接受然后;)(顺便说一句,如果我可以很快解决你的问题,那是因为我已经面对它之前大声笑:P) – Julo0sS