帮助SQL语法错误消息

时间:2010-02-16 14:03:06

标签: php sql

<?php

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["idc"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["idc"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["idc"], $_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty){


$result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

mysql_query("INSERT INTO cart(cookieId, id, qty) values('" . GetCartId() . "', $itemId, $qty)");
//printf ("Inserted records: %d\n", mysql_affected_rows());

}
else
{
// This item already exists in the users cart,
// we will update it instead
 mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");

}

}

function UpdateItem($itemId, $qty)
{

mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
//printf ("Updated records: %d\n", mysql_affected_rows());

}

function RemoveItem($itemId) 
{

mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
}

?>

<?php 
function ShowCart()

{
    $result = mysql_query("SELECT
                  cart.id         cart_id,
                  cart.id         cart_id,
                  cart.cartId     cartId,
                  cart.cookieId   cookie_Id,
                  cart.qty        qt_y,                     
                  cdkb.id         cdkb_id,
                  cdkb.name       name,
                  cdkb.image      image,
                  cdkb.price      price,
                  dkb.id          dkb_id,
                  dkb.name        name1, 
                  dkb.image       image1,
                  dkb.price       price2,
                  dbl.product_id  product_id,
              dbl.price       price3,
              dbl.variety     variety,
              dbl.description description                  
FROM
        cart


        LEFT OUTER JOIN cdkb
           ON cart.id = cdkb.id    

        LEFT OUTER JOIN dkb
           ON cart.id = dkb.id

        LEFT OUTER JOIN dbl
           ON dbl.id = dkb.id 


    WHERE
        cart.cookieId ='" . GetCartId() . "' ' ORDER BY cdkb.name AND dkb.name ASC");

    <div id="cart">
<div id="group">
<div id="quantity">Qty</div>
<div id="cartpic">Pic</div>
<div id="product">Product</div>
<div id="cartprice">Price</div>
<div id="remove">Remove</div>
</div>
<?php

$totalCost=0;
while($row = mysql_fetch_array($result))
{  
// Increment the total cost of all items
$totalCost += ($row["qt_y"] * $row["price1"]);

?>

<div id="cart1">
<select name="<?php echo $row["ckb_id"];?>" onChange="UpdateQty(this)">
<?php  print($row["ckb_id"]);?>
<?php

for($i = 1; $i <= 30; $i++)
{
echo "<option ";
if($row["qt_y"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</div>
<div id="cart2">
<img src="images/logopic.gif"<?php /*?><?php echo $row["image1"]; ?><?php */?> alt="we" width="60" height="50" />
</div>
<div id="cart3"><p><?php echo $row["dishname1"]; ?></p></div>



<div id="cart4"><p>
$<?php echo number_format($row["price3"], 2, ".", ","); ?></p></div>

<div id="cart5">
<p><?php
printf('<a href="cart.php?action=remove_item&id=%d&idc=%d&register=%s">Remove</a>', $_GET['id'], $row['ckb_id'], $_GET['register']);
?></p></div>

<hr size="1" color="red" >

<script language="JavaScript">

function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;

document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}

</script>





<?php
}
?>

<font face="verdana" size="2" color="black" style="clear:right;">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>


<?php
}
?>

以上是cart.php的更新代码和查询。如果您在查询下方注意到有一个while循环。现在,while循环完美地设置为dkb表,这些是来自page1.php的项目。这个循环的设置方式是它将重复保存,显示和管理ecah项目数量,图像,名称,价格和删除选项,显示它们并重复它们用户选择的项目。那些字段设置和设计并不完全适用于来自page2.php的项目,其中显示在购物车中的信息从表格dkb和dbl中拉出来。现在的问题是,当从page2.php到购物车时,相同的项目名称会重复三次,因为在dbl表中设计如下。

INSERT INTO `dbl` (`id`, `price`, `variety`, `description`) VALUES
(1, 20.30, 'Small Tray', 'Serves 6 to 8 people' ),
(1, 25.90, 'Medium Tray', 'Serves 12 to 15 people'),
(1, 30.90, 'Large Tray', 'Serves 18 to 21 people'),
(136, 0.00, 'Small Tray', '', 'small'),
(136, 0.00, 'Medium Tray', '' ),
(136, 0.00, 'Large Tray', ''),
(2, 0.00, 'Small Tray', ''),
(2, 0.00, 'Medium Tray', ''),
(2, 0.00, 'Large Tray', '');

上面的插入假设是dkb的三个项目,请参阅dbl.id,它在查询dbl.id = dkb.id中的dkb.id上加入了dbl.id对dkb表中的一个项目有三个价格,其中包含案例是项目1的三个价格,项目136的三个价格和项目2的三个价格。因此它是三种不同的品种,并且描述了dkb中的每个项目。从现在开始,如果你看一下在查询下面的while循环中分配的字段,其中一些是来自cdkb和dbl的其他dkb。 Rigth现在正如我所说的那样,来自dkb表的信息来自while循环中的设置,只有一个价格和一个名称。我想的是安排一个if语句条件来检测信息来自page1.php这是一个名称和价格,还是来自page2.php,它将是一个名称,从一到三个价格和品种取决于用户选择的小型,中型或大型托盘。每个托盘都有一个价格,但它将是一个名称1,136或2,这些是来自dkb表的id。在while循环中的if语句中显示来自page2.php的信息,需要一到三个价格和变量将有一个foreach循环或者一个for循环不知道用户在第2页中选择多少种。 php使用if语句帮助将其显示在购物车中的while循环中,该语句将检测从page1.php或page2.php中选择的天气。

$result = mysql_query("SELECT * FROM .... LEFT OUTER JOIN .... WHERE") // This is the query we have discussed in this forum.
 while($row = mysql_fetch_array($result))
{  if($row['cdkb_id']){
echo "<div>qty</div>";
echo"<div>". $row['image'] . "</div>";
echo"<div>". $row['name'] . "</div>";
echo"<div>". $row['price'] . "</div>";
echo"<div>remove</div>";
}
else{
echo "<div>qty</div>";
echo"<div>". $row['image1'] . "</div>";
echo"<div>". $row['name1'] . "</div>";
foreach ($row['dkb_id'] as $variety) {
echo"<div>".$variety['variety']. $variety['price3'] . "</div>";
// the vriety and price3 field will display three times as in table dbl has three prices and three variety. It is correct to use a foreach loop for  in this case?}
echo"<div>remove</div>"; 
// end of foreach loop inside the else statement
}// end of if else statement
}// end of while loop
?>

因此,最终显示和插图将被搜索并作为最终产品呈现如下,这是所需的外观。

 while($row = mysql_fetch_array($result))
{  
if (table cdkb) { 
  [1]qyt         image      name          price        remove
     1            ---       marina        $18.90        remove?    
     }end of if statement
Else table dkb {
   1]qyt            Image         Name             Price                  Remove
     1               ---         marina         Small Tray  $18.90        remove? 
                                                Medium Tray $30.24
                                                Large Tray  $35.90
                  } // end of else statement

                  }//end of while loop

请给我最后一手。

3 个答案:

答案 0 :(得分:4)

在我看来,你之后有太多的逗号:

dbl.id dbl_id

编辑:查询中的另一个错误是您正在引用表 dbl 中的字段:

dbl.id dbl_id

但您没有将该表加入查询,因此dbl.id是一个未知实体。

答案 1 :(得分:2)

您的SELECT在最后一个字段的末尾有逗号。你应该删除那个逗号。

SELECT
cart.id    cart_id,
dkb.id     dkb_id,
cdkb.id    cdkb_id,
dbl.id     dbl_id, <-- comma here isn't needed

答案 2 :(得分:0)

如果未设置$ _GET ['is'],您的查询也会爆炸。您将值null赋给$ is和$ ic,而不是字符串“null”。字符串上下文中的空值是一个空字符串(''),因此您最终会得到ON ( cart.id = dkb.id and dkb.id = )这是一个语法错误。

您必须将初始分配更改为以下内容:

$ic= isset($_GET['is']) ? '= ' . (int)$_GET['is'] : 'is null';