0

hey i am trying to add a products from different product pages into one shopping cart for example if i chose a product from asus page and another product from acer page it will display both of them in one shopping cart but now i get an error which says

select name from asus where serial=1

Column 'name' in field list is ambiguous

I created different table for each product

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

This is the function for getting the name and price :

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

This is the function for addtocart,product exist and 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;
}

This is for add to cart button in every product page:

 <button type="button" title="Add to Cart" class="button btn-cart"onclick="addtocart(<?php echo $row['serial']?>)" />

This for the shopping cart page:

<?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?> 
Abdallah
  • 198
  • 16
  • 1
    Is there a reason you're not using one table with `serial`, `name`, `price`, `picture`, and `brand`? – Rose Kunkel Apr 18 '14 at 16:36
  • Not an answer, but as an FYI, it's best to not use reserved keywords, such as "name" in a column or table name. It causes all sorts of problems. Come to think of it, this COULD be the answer. Try "select [name] from asus where serial=1" and see if that fixes it. – David Apr 18 '14 at 16:37
  • no not really haven't thought about it ...but if i was going to use brand how will i call it – Abdallah Apr 18 '14 at 16:40
  • re my comment above, that syntax was for sql server. See this instead for MySql - escaping reserved words from select queries...: http://stackoverflow.com/questions/3993465/selecting-a-column-that-is-also-a-keyword-in-mysql – David Apr 18 '14 at 16:42

1 Answers1

0

See answer here: SQL Query From 2 Tables Using Multiple Aliases

I would suggest combining all three tables into one combined Product table, with a Flag for what type of Product it is.

If not, then you need to use table 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'];
}
Community
  • 1
  • 1
divan
  • 29
  • 2
  • thank you divan but it give this error select name from products where serial=1 Incorrect parameter count in the call to native function 'isnull' – Abdallah Apr 18 '14 at 16:49