【发布时间】:2012-04-27 23:58:22
【问题描述】:
我有一个电子商务网站项目,这里是细节,这只是我无法解决的小问题。我有两张桌子,第一张桌子叫做物品,第二张桌子是购物车。我在一个页面中链接了名为 Add to cart 的链接,将我转换为 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>
如果用户点击链接,action = "add" 和产品 id 将转换为 cart.php, 问题是 $action 无法插入到 Switch 语句中,尽管我已经 echo $action 并打印添加 我不知道为什么它没有进入switch statment 我也尝试回显 $id 并打印 2 这是正确的 我已经尝试过 var_dump($_GET) 并且它也有效,它告诉我有字符串添加和 id =2 我想发疯,因为我一整天都在笔记本电脑上设置 2 并且它不起作用。
在这里,我不与购物车的会话或用户进行交互,这就是为什么在添加情况下我刚刚插入 1 作为 uid 的第一个值,如果它成功,我将添加会话。 按下添加到卡后,它将直接转移到我们找到编辑和删除的显示案例 当然,在展示柜中按下移除它会将他转移到移除案例 然而
如果他在展示案例中点击编辑链接,它将转移到他可以编辑数量的展示更新案例,如果id匹配id文本框将生成其他产品将没有文本框,如果生成文本框,默认数量将为1保存链接会出现,这会改变我们更新购物车表中数量的更新情况。
我只是想知道为什么$action 不能进入切换状态??
【问题讨论】:
-
ConnectToDb( )函数可能没有返回或静默失败。在它之后放置一个打印语句,以确保您从它返回。此外,您应该添加一个默认大小写(请参阅the docs)以查看您是否没有正确匹配大小写。 -
又一集Bobby Tables
-
出于调试目的,尝试向开关添加默认值并在其中打印 $action 的值
-
很抱歉,但不禁想知道:您的所有链接是否真的在代码中是这样写的:cart.php? action=add & id=2(我的意思是,参数周围有空格)?
-
如果我删除链接中的空格,这就是问题
标签: php switch-statement