2014-04-18 72 views
0

嘿,我試圖從不同的產品頁面添加產品到一個購物車,例如,如果我從華碩頁面和另一個產品從宏碁頁面選擇產品,它將顯示在一個購物車,但現在我得到它說將多個產品添加到一個購物車

選擇名來自華碩的錯誤的位置在字段列表串行= 1

列「名」不明確

我創建了不同的表中的每個產品

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的功能,產品的存在並獲得總:

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; 
} 

This對於添加到購物車按鈕,在每一個產品頁面:

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

是不是有一個原因,你沒有使用'serial','name','price','picture'和'brand'一張表? –

+0

不是答案,但作爲參考,最好不要使用保留關鍵字,如列名或表名中的「名稱」。它會導致各種問題。來想一想,這可能是答案。嘗試「從串行= 1的asus中選擇[名稱]」,並查看是否修復它。 – David

+0

沒有沒有真的沒有想過它......但如果我打算使用品牌,我將如何將其稱爲 – Abdallah

回答

0

請參閱回答這裏: SQL Query From 2 Tables Using Multiple Aliases

我建議所有三個表組合成一個組合產品表,帶有一個標誌表示它是什麼類型的產品。

如果沒有,那麼你需要使用表別名。

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']; 
} 
+0

謝謝沙發,但它給這個錯誤選擇產品的名稱,其中序列= 1 調用本地函數'isnull'不正確的參數計數 – Abdallah

+0

感謝您幫助我做到了 – Abdallah

相關問題