2015-04-17 67 views
1

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> 

我越来越被称为未定义的变量会话的错误,我不是很确定我需要定义来解决这个问题是什么样的变量?或者是我连接到数据库的方式?任何可能会给我足够的帮助和提示的信息将不胜感激。 :)

+4

删除您$ ...只是在session_start(); – Julo0sS

+0

omg,谢谢..哈哈。即时通讯这样的新手 – johnobc

+0

标记答案接受然后;)(顺便说一句,如果我可以很快解决你的问题,那是因为我已经面对它之前大声笑:P) – Julo0sS

回答

1

使用$的PHP变量... 当您使用的功能,你不需要用$ , 所以在这里打开它上面的所有代码...

//... 
session_start(); 
//... 

会更好....

1

尝试打开该HTML之前的会话,并删除$

像这样:

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

使用session_start();而不是美元符号,因为您调用的函数不访问变量。作为一个方面说明:“要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。”更多这里:http://php.net/manual/en/function.session-start.php

1

请使用下面的代码简化您的代码。 '$'不应该用在你的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>