doujiling4377 2013-06-15 14:50
浏览 33
已采纳

使用“会话”将项目添加到购物车

I have been following a tutorial for a php shopping cart,

I have checked my code over and I am to the point where the add to cart button SHOULD be adding the products into the sidebar.

However, it appears to skip over my if statement and go straight to the else error message stating the product ID is invalid. I have checked that the SKU in the database match thought that are displayed in $id so i'm a little lost as to why this error persists?

PHP for Products:

<?php
session_start();

if (isset($_GET['action']) && $_GET['action'] == "add") {
    $id = $_GET['id'];
    if (isset($_SESSION['cart'][$id])) {
        $_SESSION['cart'][$id]['quantity']++;
    } else {
        $sql2 = "SELECT * FROM products WHERE SKU=$id";
        $query2 = mysql_query($sql2);

        if(mysql_num_rows($query2) != 0){
            $row2 = mysql_fetch_array($query2);
            $_SESSION['cart'][$row2['SKU']] = array("quantity" => 1, "price" =>      $row2['price']);

        } else {
            $message = "This product ID is invalid";
        }
    }
}

?>

<h2 class="message"><?php if(isset($message)){echo $message;} ?></h2>
<h1>Product Page</h1>
<table>
  <tr>
    <th>Name</th>
    <th>Description</th>
    <th>Price</th>
    <th>Action</th>
  </tr>

<?php
$sql = "SELECT * FROM products ORDER BY SKU ASC";
$query = mysql_query($sql)or die(mysql_error());

while($row = mysql_fetch_assoc($query)){
?>

  <tr>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['description']; ?></td>
    <td><?php echo "&pound;" . $row['price']; ?></td>
    <td><a href="index.php?page=products&action=add&id=<?php echo $row['SKU']; ?>">Add to cart</a></td>
  </tr>

<?php
}
?>

PHP for Index.php:

<?php
    session_start();

    require_once("connect.php");
    if (isset($_GET['page'])) {
        $pages = array("products","cart");
        if (in_array($_GET['page'],$pages)) {
            $page = $_GET['page'];
        } else {
            $page = "products";
        }
    } else {
        $page = "products";
    }

?>

    <html>
      <head>
        <link rel="stylesheet" href="reset.css" />
        <link rel="stylesheet" href="style.css" />
        <title>Shopping Cart - WebThatWorks Ltd</title>
      </head>

      <body>
        <div id="container">
        <div id="main"><?php require($page. ".php"); ?></div>
        <div id="sidebar">

        <h1>Cart</h1>
<?php
          if (isset($_SESSION['cart'])) {
              $sql = "SELECT * FROM products WHERE SKU IN (";
              foreach ($_SESSION['cart'] as $id => $value) {
                  $sql .=$id. ",";
              }
              $sql = substr($sql,0,-1) . ")ORDER BY SKU ASC";
              $query = mysql_query($sql);
              while($row = mysql_fetch_array($query)){
?>
                <p><?php echo $row['name']; ?><?php echo $_SESSION['cart'][$row['SKU']]['quantity']; ?></p>
                <a href="index.php?page=cart">Go To Cart</a>
<?php
              }
          } else {
              echo "<p>Your Cart Is Empty.  <br /> Please Add some products</a>";
          }
?>

If you require me to post the structure of my database, I shall do so

  • 写回答

1条回答 默认 最新

  • dongxi0605 2013-06-15 15:24
    关注

    The problem is your SKU is a string and so it needs quotes in the query:

    $sql2 = "SELECT * FROM products WHERE SKU='" . mysql_real_escape_string($id) . "'";
    

    I also added the escape call to prevent SQL Injection, but for best security and other benefits, you should switch to a modern API such as PDO or MySQLi and use prepared statements.

    You will also need to make sure the quotes are added to the IN query as well.

    foreach ($_SESSION['cart'] as $id => $value) {
        $sql .="'" . mysql_real_escape_string($id) . "',";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等