請幫助我。這是情況。MySQL - SQL語法錯誤
我有一個購物車,一切工作正常。唯一的一點是,我得到錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND p.CatId = c.CatId AND ws.ProdId = p.ProdId' at line 1
基本上,我想是從購物車中刪除單品,當只有1個產品,通過點擊按鈕,旁邊的數量文本框。產品從購物車中刪除,但會引發上述錯誤。
這裏是我的代碼:
<?php
if (isset($_SESSION['cart']) && $_SESSION['cart'] != "") {
$total = 0;
$subTotal = 0; $sbTotal = 0;
$taxAmount = $tax = $totalTaxAmount = $taxAmt = 0;
$cartWeightPerProduct = $totalCartWeight = $amtWeight = 0;
$sql = "SELECT p.*, c.*, ws.* FROM products p, categories c, weight_shipping ws WHERE ProdCode IN (";
foreach ($_SESSION['cart'] as $id => $value) {
$sql .= '"'.$id.'",';
}
$sql = substr($sql, 0, -1) . ") AND p.CatId = c.CatId AND ws.ProdId = p.ProdId";
if ($validate->Query($sql) == TRUE) {
if ($validate->NumRows() >= 1) {
while ($row = $validate->FetchAllDatas()) {
echo '<tr>';
echo '<td data-title="Product Image & name" class="t_md_align_c"><img src="images/Products/'.$row['ProdCode'].'.jpg" alt="'.$row['ProdCode'].'" class="m_md_bottom_5 d_xs_block d_xs_centered" height="75" width="75"><a href="product.php?code='.$row['ProdCode'].'" class="d_inline_b m_left_5 color_dark">'.$row['ProdName'].'</a></td>';
echo '<td data-title="SKU">'.$row['ProdCode'].'</td>';
echo '<td data-title="Price"><p class="f_size_large color_dark">Rs. '.$row['ProdRate'].'</p></td>';
echo '<td data-title="Quantity"><div class="clearfix quantity r_corners d_inline_middle f_size_medium color_dark m_bottom_10"><form action="cart.php" method="POST"><input type="number" max="99" min="0" name="qnty['.$row["ProdCode"].']" value="'.$_SESSION['cart'][$row['ProdCode']]['quantity'].'" class="f_left"><br /><button type="submit" name="btnSubmit" class="f_left"><span class="fa fa-refresh"></span></button></form><form action="removeProduct.php" method="POST"><input type="hidden" name="remove['.$row["ProdCode"].']" value="'.($row['ProdRate'] * $_SESSION['cart'][$row['ProdCode']]['quantity']).'"><button type="submit" name="btnRemove" class="f_right"><span class="fa fa-times"></span></button></form></div></td>';
$sbTotal = $row['ProdRate'] * $_SESSION['cart'][$row['ProdCode']]['quantity'];
$subTotal = $sbTotal;
echo '<td data-title="Subtotal"><p class="f_size_large fw_medium scheme_color t_align_r">'.number_format($sbTotal, 2).'</p></td>';
$total += $subTotal;
$_SESSION['cartTotalAmount'] = $total;
$tax = $row['CatTaxPercent'];
$taxAmt = (($sbTotal * $tax)/100);
$taxAmount += $taxAmt;
$amt = 0;
$cartWeightPerProduct = ($row['weight'] * $_SESSION['cart'][$row['ProdCode']]['quantity']);
echo '</tr>';
$totalCartWeight += $cartWeightPerProduct;
}
$totalTaxAmount += $taxAmount;
$_SESSION['cartWeight'] = $totalCartWeight;
$_SESSION['sessionTotalPayable'] = ($total + $totalTaxAmount);
$_SESSION['Presentation']['TotalPayableAmount'] = $_SESSION['sessionTotalPayable'];
if (isset($_SESSION['sessionTotalPayable'])) {
$amt = $_SESSION['sessionTotalPayable'];
} else {
$amt = "Rs. 0";
}
echo '<tr><td colspan="4"><p class="fw_medium f_size_large t_align_r t_xs_align_c">Cart Total:</p></td><td colspan="1"><p class="fw_medium f_size_large color_dark t_align_r">'.number_format($total, 2).'</p></td></tr>';
echo '<tr><td colspan="4"><p class="f_size_large t_align_r t_xs_align_c">Taxes:</p></td><td colspan="1"><p class="f_size_large color_dark t_align_r">'. number_format($totalTaxAmount, 2) .'</p></td></tr>';
echo '<tr><td colspan="4"><p class="f_size_large t_align_r t_xs_align_c">Cart Weight:</p></td><td colspan="1"><p class="f_size_large color_dark t_align_r">'. $totalCartWeight .' grams</p></td></tr>';
echo '<tr><td colspan="4"><p class="f_size_large t_align_r t_xs_align_c">Shipment Fee </p></td><td colspan="1"><p class="f_size_large color_dark t_align_r" id="amt"><button id="amoutn" data-popup="#clickMeForGettingShippingAmount" class="tr_delay_hover r_corners button_type_16 f_size_medium bg_scheme_color color_light m_xs_bottom_5">Estimate</button></p></td></tr>';
echo '<tr><td colspan="4"><p class="fw_medium f_size_large t_align_r t_xs_align_c">Total Payable Amount:</p></td><td colspan="1"><p class="fw_medium f_size_large color_dark t_align_r">'.number_format(($total + $totalTaxAmount), 2).'</p></td></tr>';
echo '<tr><td colspan="4"><div id="chckOutBtn"><button class="f_right tr_delay_hover r_corners button_type_16 f_size_medium bg_scheme_color color_light m_xs_bottom_5">Proceed to Checkout</button></div></td><td colspan="1"><p class="fw_medium f_size_large color_dark t_align_r"><a href="EmptyCart.php">Empty Cart</a></p></td></tr>';
}
}
} else {
echo 'Your Cart Is Empty';
}
後Jonathan Dahan評論,我已在if
聲明更改的變化:
if (isset($_SESSION['cart']) && $_SESSION['cart'] != "") {
到
if (isset($_SESSION['cart']) && is_array($_SESSION['cart']) && sizeOf($_SESSION['cart']) > 0) {
越來越其表像
p.ProdCode
你嘗試打印出整個SQL SELECT執行前?又名'echo $ sql;'? –是的,我做了:這裏是'$ sql'的輸出:'SELECT p。*,c。*,ws。* FROM products p,categories c,weight_shipping ws WHERE ProdCode IN(「PROD-1539」)AND p。 CatId = c.CatId AND ws.ProdId = p.ProdId' –
如果$ _SESSION ['cart']爲空,將會發生此錯誤,因爲它最終會執行** ... WHERE ProdCode IN()AND。 .. **這是不正確的語法 – Augwa