Hi everyone..

I'm using a very simple code to remove an item from session array. I just jave no idea why it returns SQL error like this in another page that initiated the deletion:
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 ')' at line 1

my code to remove is as below:

<?php
session_start();
$id = $_GET['id']; //product marked for deletion on the webpage
unset($_SESSION['items'][$id]);
header('Location:add_cart.php');
?>

Recommended Answers

All 7 Replies

That code sample clearly isn't making any SQL queries so you'll need to look at your code that is.
Presumably at some point you are building up an SQL query that is being incorrectly structured because it is attempting to concatenate a null string.
I'm guessing that is why the error refers to ')'.
Post up the actual SQL if you can't see the problem.

provide SQL , if possible.
the information you provided is not enough to debug your error. Be brief while explaining your problem.

mysql queries as below:
//below is to display image and carname

                  mysql_select_db($database);

                  $result_car=mysql_query($query_car);
                  while($row_car=mysql_fetch_array($result_car))
                  {
                      $image=$row_car['gallery'];
                      $car_name=$row_car['carName'];
                      $make=$row_car['carMake_id'];
                      echo $car_name;
                      ?>
                      <img src="management/uploads/<?php if(isset($make)){echo $make;} ?>/<?php echo $image; ?>"  width="150" height="100"/>
                      <?php
                  }



                  ?>

// below is to display addon items ,their deposit and price perday and total

 <?php 
                 $colname_RecAdd_on = $id['add_on'];
                mysql_select_db($database);
                 $query_add= sprintf("SELECT * FROM tbl_addons WHERE addOns_id IN (%s)", $colname_RecAdd_on);

                 $result_add=mysql_query($query_add)or die(mysql_error());
                   $sub_sum=array();
                 while($row_add=mysql_fetch_array($result_add))
                 {
                      echo '<hr> <span style="color:#FF0055">'.$row_add['addOns']." </span><hr>";
                     echo "<br/>Deposit=RM ".$row_add['Deposit'];
                      echo "<br/>Price per Day=RM ".$row_add['PricePerDay']."<br/>";


                      $sub_sum=$row_add['Deposit']+$row_add['PricePerDay'];

                      $sum_addOn+=$sub_sum;
                    echo '<br/><span style="color:#FF0055">sub-total=RM '.$sub_sum.'</span><br/><br/>';
                    //echo "<br/>Total=RM ".$sub_sum."<br/>";

                 }
                 ?>
                 <?php          
                if(isset($total))
                                {
                                    $sum+=$total;
                                }


                 //$grand_sum=$sum+$row_add['Deposit']+$row_add['PricePerDay'];


                 ?>

// I have also edited the remove.php page like below:

<?php
session_start();
$id=$_GET['id'];

$cart=$_SESSION['items'];


        foreach($cart as $key=>$items)
{
  $items['total'];
  //echo $items['add_on'];
  $add=$items['add_on'];

  if(isset($id)==($items['total']))
  {
      unset($_SESSION['items'][$id]);

      header("Location:add_cart.php");
  }


}

?>

Now the problem is,i's deleting only the id and not addon. So I tried adding
unset($_SESSION['items'][$add]);
to remove addon items as well but it's not removing correctly. It removes all items and when the cart it empty shows sql error.

You still haven't given us everything required. We know the error is coming from an SQl query and in your first code segment there is :
$result_car=mysql_query($query_car);
with no reference to what $query_car is.

Echo your SQL statements just before you run the query and check the syntax.

Steve, Thanks..I'm not sure which part I missed from posting..so here I'm posting the entire script in add_cart.php..Hopefully can help

<?php
session_start();
$car_id=0;

if(isset($_GET['id']))
{
    $car_id=$_GET['id'];
}
$price=0;
if(isset($_GET['price']))
{
    $price=$_GET['price'];
}
$add_on=0;
if(isset($_GET['addon']))
{
    $add_on=$_GET['addon'];
}

if(isset($_GET['dep']))
{
    $dep=$_GET['dep'];
}
if(isset($_GET['location']))
{
    $location=$_GET['location'];
}

include'management/connection.php';
mysql_select_db($database);

if(!isset($_SESSION['items']))
{
    $_SESSION['items']=array();
}

$_SESSION['items'][$price]=array('total'=>$car_id,'add_on'=>$add_on);

//select carName,gallery

/*mysql_select_db($database);
$query="SELECT tbl_car_name.*, tbl_gallery.*,tbl_car_make.* FROM tbl_car_name,tbl_gallery,tbl_car_make WHERE tbl_car_name.carName_id='$car_id' AND tbl_car_make.carMake_id=tbl_car_name.carMake_id AND tbl_gallery.carName_id='$car_id'";

$result=mysql_query($query)or die(mysql_error());
while($row=mysql_fetch_array($result))
{
    $carName=$row['carName'];
    $gallery=$row['gallery'];
    $carMake_id=$row['carMake_id'];
    $car[$row['carName']][]=$row['gallery'];

}
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Description" content="">
<meta name="KeyWords" content="">
<meta name="viewport" content="width=device-width">

<link rel="stylesheet" type="text/css" href="style.css" />
<title></title><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width">

<link rel="stylesheet" type="text/css" href="style.css" />

</head>


<body>
    <div id="container">
        <div class="header">
            <div class="logo"><img src="acr-logo-1.jpg" width="200" height="100"></div>
            <div class="social">
                <?php include'social.php'; ?>
             </div>
            <div class="menu">

            <?php include 'menu.php';?>      

            </div>
        </div>
        <div class="body">
          <div class="page-content">

          <h2><span style="padding-left:20px;padding-top:20px;">In Your cart</span></h2>

          <?php 
          if(isset($car_id))
          {
          echo "<span style='padding-left:20px;'>Number of Items in the cart = ".sizeof($_SESSION['items'])." </span><br>";
          }
          ?>
            <table class="content" border="1" style="border-collapse:collapse;width:90%;margin-left:20px;">

                <tr><th>price</th><th>Item</th><th>Extras</th><th></th></tr>
            <?php
         foreach($_SESSION['items'] as $total=>$id)
          {

              ?>
              <tr>

                  <td width="200" valign="top"> <span style="color:#FF0055"><?php echo 'RM '.$total;?></span></td>             
                  <td width="100" valign="top">
                  <!--car id-->

                  <span style="color:#FF0055"><?php  $c_id=$id['total'];
                 mysql_select_db($database);
                  $query_car="SELECT tbl_car_name.*,tbl_gallery.*,tbl_car_make.* FROM tbl_car_name,tbl_gallery,tbl_car_make WHERE tbl_car_name.carName_id='$c_id' AND tbl_gallery.carName_id=tbl_car_name.carName_id AND tbl_car_name.carMake_id=tbl_car_make.carMake_id";
                  $result_car=mysql_query($query_car);
                  while($row_car=mysql_fetch_array($result_car))
                  {
                      $image=$row_car['gallery'];
                      $car_name=$row_car['carName'];
                      $make=$row_car['carMake_id'];
                      echo $car_name;
                      ?>
                      <img src="management/uploads/<?php if(isset($make)){echo $make;} ?>/<?php echo $image; ?>"  width="150" height="100"/>
                      <?php
                  }



                  ?>
                  </span>
                  </td>
                  <td width="450">

                  <?php  if(isset($id['add_on'])){ /*echo $id['add_on'];*/} ?>
                 <?php 
                 $colname_RecAdd_on = $id['add_on'];
                mysql_select_db($database);
                 $query_add= sprintf("SELECT * FROM tbl_addons WHERE addOns_id IN (%s)", $colname_RecAdd_on);

                 $result_add=mysql_query($query_add)or die(mysql_error());
                   $sub_sum=array();
                 while($row_add=mysql_fetch_array($result_add))
                 {
                      echo '<hr> <span style="color:#FF0055">'.$row_add['addOns']." </span><hr>";
                     echo "<br/>Deposit=RM ".$row_add['Deposit'];
                      echo "<br/>Price per Day=RM ".$row_add['PricePerDay']."<br/>";


                      $sub_sum=$row_add['Deposit']+$row_add['PricePerDay'];

                      $sum_addOn+=$sub_sum;
                    echo '<br/><span style="color:#FF0055">sub-total=RM '.$sub_sum.'</span><br/><br/>';
                    //echo "<br/>Total=RM ".$sub_sum."<br/>";

                 }
                 ?>
                 <?php          
                if(isset($total))
                                {
                                    $sum+=$total;
                                }


                 //$grand_sum=$sum+$row_add['Deposit']+$row_add['PricePerDay'];


                 ?>
                  </td>
                  <td width="100"><a href="remove.php?id=<?php echo $c_id;?>&action=delete">remove<?php echo $c_id;?></a></td>

              </tr>


        <?php       
          }
        ?>
           <tr>
              <td><strong><i> <span style="color:#FF0055">Grand Total:</span></i></strong></td><td colspan="3"><strong><i> <span style="color:#FF0055">RM <?php echo $sum + $sum_addOn;?> </span></i></strong></td>
              </tr>

            </table>    
            <a href="cars.php" style="padding-left:20px;">Back to cars .....</a>&nbsp; 
             <a href="clear.php">Empty Cart</a>
           <?php
           if(isset($_POST['submit']))
           {
               $payment_method=$_POST['p_method'];
               echo $payment_method;
               $c_name=$_POST['name'];
               echo  $c_name;
               $c_remark=$_POST['remark'];
               echo $c_remark;
               echo $_GET['location'];

               mysql_select_db($database);
               $insert="INSERT INTO tbl_order(customer_name,remark_by_customer,pay_type,Create_Dt) VALUES ('$c_name',' $c_remark','$payment_method',now())";
               mysql_query($insert)or die(mysql_error());

               header('Location:checkout.php');


           }

           ?>

            <form action="" method="post">
            <table style="padding-top:30px;padding-left:20px;padding-bottom:30px;">
            <tr>
            <td valign="top">Payment method</td>
            <td valign="top">:</td>
            <td valign="top">
             <select name="p_method">
             <option value="1">Direct bank transfering</option>
              <option value="2">Credit Card</option>
             <option value="3">Paypal</option>


             </select>
            </td>
            </tr>
            <tr>
            <td valign="top">Customer Name</td>
            <td valign="top">:</td>
            <td>
            <input type="text" name="name" />
            </td>
            </tr>
           <tr>
            <td valign="top">Customer Remark</td>
            <td valign="top">:</td>
            <td>
            <textarea name="remark" cols="30" rows="5"></textarea>
            </td>
            </tr>
            <tr>
            <td colspan="3"><input type="submit" name="submit" value="submit"  onclick="return confirm('Do you really want to proceed to checkout?');"href="checkout.php" style="padding-left:20px;"/>
            </td>
            </tr>

            </table>
            </form>
          </div>
          <div class="content3">
            <?php include'footer.php'; ?>
          </div>
         <div  class="content4">


          </div>
        </div>

    </div>
</body>
</html>

The only query I can see that might cause the error you are seeing is this one:
$query_add= sprintf("SELECT * FROM tbl_addons WHERE addOns_id IN (%s)", $colname_RecAdd_on);
If $colname_RecAdd_on is empty then your SQL becomes
SELECT * FROM tbl_addons WHERE addOns_id IN ()
Which matches your error. I would confirm that
colname_RecAdd_on = $id['add_on'];
is an actual value.

I can now remove the items individually from the session but only the last item remain there.Why is it so?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.