嘿,我正在尝试将来自不同产品页面的产品添加到一个购物车中,例如,如果我从华硕页面选择一个产品,而从宏碁页面选择另一个产品,它将在一个购物车中显示这两个产品,但现在我收到一个错误,显示如下
从asus where serial=1中选择名称
字段列表中的列'name‘不明确
我为每个产品创建了不同的表
CREATE TABLE asus ( serial int(11), name varchar(20), price float(), picture varchar(80) );
CREATE TABLE acer ( serial int(11), name varchar(20), price float(), picture varchar(80) );
CREATE TABLE lenovo ( serial int(11), name varchar(20), price float(), picture varchar(80) );
这是获取名称和价格的函数:
function get_product_name($pid){
$result=mysql_query("select name from asus,acer,lenovo
where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['name'];
}
function get_price($pid){
$result=mysql_query("select price from asus,acer,lenovo where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['price'];
}这是addtocart,product exist和get total的函数:
function get_order_total(){
$max=count($_SESSION['cart']);
$sum=0;
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$price=get_price($pid);
$sum+=$price*$q;
}
return $sum;
}
function addtocart($pid,$q){
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($pid)) return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$pid;
$_SESSION['cart'][0]['qty']=$q;
}
}
function product_exists($pid){
$pid=intval($pid);
$max=count($_SESSION['cart']);
$flag=0;
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['cart'][$i]['productid']){
$flag=1;
break;
}
}
return $flag;
}这是用于每个产品页面中的添加到购物车按钮:
<button type="button" title="Add to Cart" class="button btn-cart"onclick="addtocart(<?php echo $row['serial']?>)" />这是购物车页面的内容:
<?php
if(is_array($_SESSION['cart'])){
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$pname=get_product_name($pid);
$price=get_price($pid);
if($q==0) continue;
?>
<tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td>
<?php echo $pname?>
</td>
<td>RM
<?php echo $price?> 发布于 2014-04-19 00:43:47
请在此处查看答案:SQL Query From 2 Tables Using Multiple Aliases
我建议将这三个表合并到一个组合的Product表中,并带有一个标识它是什么类型的Product的标志。
如果不是,则需要使用表别名。
function get_product_name($pid){
$result=mysql_query("select isnull(isnull(a.name, c.name), l.name) as name from asus a,acer c,lenovo l
where(a.serial=$pid OR c.serial=$pid OR l.serial=$pid) ") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['name'];
}https://stackoverflow.com/questions/23158209
复制相似问题