-5

我有一个电子商务网站项目,这是细节,这只是我无法解决的小问题。我有两张桌子,第一张桌子叫做物品,第二张桌子是购物车。我在一个页面中链接了名为“添加到购物车”的链接,将我转换为 cart.php

a href="cart.php? action=add & id=<?php echo $itemId; ?>"> Add to Cart </a><br/>

'<a href="cart.php? action=show & id=<?php echo $itemId; ?> ">View Shopping Cart </a>` 

之后我有 cart.php 页面:

  <?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<html>
<head>
<title> Cart Page </title>
</head>
<body>
<?php
include "function.php";
ConnectToDb( );
$id= $_GET['id'];
$action = $_REQUEST['action'];
$action=(isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';

switch($action){
case "add":
{$sql=mysql_query("SELECT * FROM items WHERE itemId='$id'");
 $row=mysql_fetch_array($sql);
 $itemPrice= $row["itemPrice"];
 $itemName= $row["itemName"];
 $quanty=$row["qty"];

$query="insert into cart values('1',".$_GET[id].",1,".$itemPrice.",".$itemName.")";
$result=mysql_query($query);
header("location:cart.php? action=show ");
echo "Item Was Added";
break;
}
case "remove":
{
$query="delete from cart where itemId=".$_GET['id'];
$result = mysql_query($query);
header("location:cart.php?action=show");
}

case "update":
{
$query="update cart set". $quanty = $_POST['quantity']."where itemId=".$_GET['id'];
$result = mysql_query($query);
header("location:cart.php?action=show");
break;
}
case "show_update":
{
$result = mysql_query("select * from cart");
$uid=$row["uid"];
$itemId= $row["itemId"];
$qty=$row["qty"];
$itemPrice=  $row["itemPrice"];
$itemName=  $row["itemName"];
while($row = mysql_fetch_array($result))
{   if ($itemId == $id )
    {  
            $totalCost =0;
            $query = "select * from cart inner join items on cart.itemId= items.itemId";    
            $result = mysql_query($query); 
            echo "<table width ='100%' border ='1'>"; 
            while($row = mysql_fetch_array($result)) 
            {$totalCost += ($qty * $itemPrice);
            echo "<tr>";
            echo "<td>";    echo $itemName;     echo "</td>";
            echo "<td> SR"; echo $itemPrice;    echo "</td>";
            echo "<td> <input type ='text' name='quantity' value=".$qty."</td>";
            echo'<td width="23%"><a href="cart.php?action=update&id='.$itemId.'">save</a></td>';
            echo'<td width="23%"><a href="cart.php?action=remove&id='.$itemId.'">Remove</a></td>';
            echo"</tr>";}
            // Increment the total cost of all items
            $totalCost += ($row["qty"] * $row["itemPrice"]);
            echo "<tr> <td colspan='2'> <a href='products.php'>Keep Shopping</a></td>";
            echo "<td colspan='2'> <b>Total: SR".$totalCost."</b></td></tr>";
            echo "</table>";
            }
        else{   
            $totalCost =0;
            $query2 = "select * from cart inner join items on cart.itemId= items.itemId";   
            $result2 = mysql_query($query2); 
            echo "<table width ='100%' border ='1'>"; 
            while($row2 = mysql_fetch_array($result2)) 
            {$totalCost += ($row2["qty"] * $row2["itemPrice"]);
            echo "<tr>";
            echo "<td>";    echo $row2['itemName'];     echo "</td>";
            echo "<td> SR"; echo $row2["itemPrice"];    echo "</td>";
            echo "<td>";     echo $row2["qty"];         echo "</td>";
            echo'<td width="23%"><a href="cart.php?action=show_update&id='.$row2["itemId"].">edit</a></td>";
            echo'<td width="23%"><a href="cart.php?action=remove&id='.$row2["itemId"].">Remove</a></td>";
            echo"</tr>";
            }
            // Increment the total cost of all items
            $totalCost += ($row2["qty"] * $row2["itemPrice"]);
            echo "<tr> <td colspan='2'> <a href='homestore.php'>Keep Shopping</a></td>";
            echo "<td colspan='2'> <b>Total: SR".$totalCost."</b></td></tr>";
            echo "</table>";}
            break;
}}
case "show":
{
$totalCost =0;
$query = "select * from cart inner join items on cart.itemId = items.itemId";
$result = mysql_query($query);
?>
<table width="100%" border="1"> 
    <?php while($row = mysql_fetch_array($result))
    {   
    $totalCost += ($row["qty"] * $row["itemPrice"]);
    ?>
    <tr>
    <td><?php echo $row["itemName"]; ?></td>
    <td>SR<?php echo $row["itemPrice"]; ?></td>
    <td><a href="cart.php?action=show_update&id=<?php echo $row["itemId"]; ?>">edit</a></td>
    <td><a href="cart.php?action=remove&id=<?php echo $row["itemId"]; ?>">Remove</a></td>
    </tr>
<?php } 

// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); ?>
<tr> <td colspan="2"> <a href="products.php">Keep Shopping</a></td>
<td colspan="2"> <b>Total: SR<?php echo $totalCost; ?></b></td></tr>
</table>

<?php break; }
}


?>
</body>
</html>

如果用户单击链接,则操作 =“添加”和产品 ID 将转换为 cart.php,问题是 $action 无法插入到 Switch 语句中,即使我已经回显了 $action 并且它打印添加 I不知道为什么它没有进入 switch statment 我也尝试回显 $id 并打印 2 这是正确的2 我想发疯,因为我整天在笔记本电脑上设置 2 并且它不工作。

在这里,我不与购物车的会话或用户发生冲突,这就是为什么在添加情况下我刚刚插入 1 作为 uid 的第一个值,如果它成功,我将添加会话。按下添加到卡后,它将直接转移到我们找到编辑和删除的展示案例,当然在展示案例中按删除它会将他转移到移除案例

如果他在展示案例中按下编辑链接,它将转移他到展示更新案例,他可以在其中编辑数量,如果 id 匹配 id 文本框将生成其他产品将没有文本框,默认数量将为 1 如果文本框生成保存链接它将出现改变他更新情况的情况,我们更新购物车表中的数量。

我只是想知道为什么$action不能进入 switch 语句?

4

4 回答 4

0

我找到了解决方案;如果你知道答案,你会笑的。这是:

它不应该开始, $id= $_GET['id']; 因为在“添加”到购物车“链接中,动作首先出现id
$id= $_GET['id']; 从一开始就删除了,我把它放在每个箱子里,它正在工作。谢谢你的帮助。

于 2012-04-28T22:49:34.677 回答
0

在此行之后回显 $action

$action=(isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';

并检查它的值

于 2012-04-28T00:04:50.217 回答
0

你知道这些行是做什么的吗:

'<a href="cart.php? action=show & id=<?php echo $itemId; ?> ">View Shopping Cart </a>`

$action=(isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';

如果没有,我谦虚地建议您考虑购买电子商务解决方案或使用开源选项。

如果你这样做了,那么你应该看到这实际上是你的错误源于你的查询字符串中的空格。

此外,由于您没有view分支或default分支,因此您将跳过整个switch语句。

于 2012-04-28T00:10:45.563 回答
-1

更改$_GET[id]为,$id因为您已经在 $id 中收到了这个

于 2012-04-28T00:00:32.703 回答