Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"error in your SQL syntax" on remove cart entry 1

Status
Not open for further replies.

mancroft

Programmer
Joined
Oct 26, 2002
Messages
267
Location
GB
I have done a shopping cart which works except that when I click on the remove image file:

Code:
<a href = &quot;ShoppingBag.php?removeitem=<?php echo $row[&quot;cartId&quot;]; ?>&quot;>               
<IMG src=&quot;Images/Del.jpg&quot;></A>

it removes the item from the database but then generates the error:

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

Here is the code for the page:

Code:
<?php

include(&quot;db.php&quot;);

if (!empty($_REQUEST['removeitem'])) { 
RemoveItem($_REQUEST['removeitem']); 
}


global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = mysql_connect($dbServer, $dbUser, $dbPass, $dbName) or die
(mysql_error()); 


$db = mysql_select_db(&quot;kraffs5_actuacoscart&quot;, $cxn)  or die(mysql_error()); 


$sql = (&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values
('&quot; . GetCartId() . &quot;', $ITEMID, $QTY, '$COLOR' ,'$SIZE')&quot;) or die(mysql_error
());


@mysql_query(&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values('&quot; . GetCartId() . &quot;', $ITEMID, $QTY, '$COLOR' ,'$SIZE')&quot;) or die (mysql_error()); ;

ShowCart();


function RemoveItem($cartId)

{
// Uses an SQL delete statement to remove an item from
// the users cart

global $dbServer, $dbUser, $dbPass, $dbName;


// Get a connection to the database

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());
		
mysql_query(&quot;delete from cart where cookieId = '&quot; . GetCartId() . &quot;' and cartId = $cartId&quot;) or die(mysql_error());

	}


Any ideas as to the error, please?
 
I'm sure I showed you this yesterday. Why don't you try my suggestion of creating the sql statement outside of the query and echoing it out to see if its correct?

Code:
function RemoveItem($cartId)

 {
 // Uses an SQL delete statement to remove an item from
 // the users cart

 global $dbServer, $dbUser, $dbPass, $dbName;


 // Get a connection to the database

 $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());
  
  [COLOR=red]
  //create the sql statement
  $sql=&quot;delete from cart where cookieId = '&quot; . GetCartId() . &quot;' and cartId = $cartId&quot;;

  //echo it out to test the statement for wellformedness
  echo $sql; [/color]

 //run the query
 mysql_query($sql) or die(mysql_error());

}//end function

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Item 27 was deleted from the database and the Output is this:


delete from cart where cookieId = '0edeee281dfb3b2a33f407446ca2a208' and cartId = 27You 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

Thanks for your help.
 
I'm confused.

If the query was successful, why are you getting an error?

Why does the error message you've posted mention text that does not exist in the query? The error message in question looks more like a problem with one of your INSERT queries, not with your DELETE query.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
i think the whole page needs to be looked at, it does try to execute the insert as well as the delete...let's try this:
Code:
                         <?php

                         include(&quot;db.php&quot;);

                         if (!empty($_REQUEST['removeitem'])) { 
                         RemoveItem($_REQUEST['removeitem']); 
                         [b]die();[/b] //this stops the rest of the script from running
                         }


                         global $dbServer, $dbUser, $dbPass, $dbName;

                         // Get a connection to the database
                         $cxn = mysql_connect($dbServer, $dbUser, $dbPass, $dbName) or die
                         (mysql_error()); 


                         $db = mysql_select_db(&quot;kraffs5_actuacoscart&quot;, $cxn)  or die(mysql_error()); 


                         $sql = (&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values
                         ('&quot; . GetCartId() . &quot;', $ITEMID, $QTY, '$COLOR' ,'$SIZE')&quot;) or die(mysql_error
                         ());


                         @mysql_query(&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values('&quot; . GetCartId() . &quot;', $ITEMID, $QTY,
                         '$COLOR' ,'$SIZE')&quot;) or die (mysql_error()); ;

                         ShowCart();


                         function RemoveItem($cartId)

                          {
                          // Uses an SQL delete statement to remove an item from
                          // the users cart

                          global $dbServer, $dbUser, $dbPass, $dbName;


                          // Get a connection to the database

                          $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());
                           
                           [COLOR=red]
                           //create the sql statement
                           $sql=&quot;delete from cart where cookieId = '&quot; . GetCartId() . &quot;' and cartId = $cartId&quot;;

                           //echo it out to test the statement for wellformedness
                           echo $sql; [/color]

                          //run the query
                          mysql_query($sql) or die(mysql_error());

                         }//end function

?>

another option (and my preferred way of programming is to place each function (insert and delete) into thier own function (you already have the delete). The the control for the script is a simple if/then

Code:
if (!empty($_REQUEST['removeitem'])) { 
        //run the delete
         RemoveItem($_REQUEST['removeitem']); 
                         
 }else{
     
       //run the insert
       DoInsert();

}
function DoInsert()
{
global $dbServer, $dbUser, $dbPass, $dbName;

                         // Get a connection to the database
                         $cxn = mysql_connect($dbServer, $dbUser, $dbPass, $dbName) or die
                         (mysql_error()); 


                         $db = mysql_select_db(&quot;kraffs5_actuacoscart&quot;, $cxn)  or die(mysql_error()); 


                         $sql = (&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values
                         ('&quot; . GetCartId() . &quot;', $ITEMID, $QTY, '$COLOR' ,'$SIZE')&quot;) or die(mysql_error
                         ());


                         @mysql_query(&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values('&quot; . GetCartId() . &quot;', $ITEMID, $QTY,
                         '$COLOR' ,'$SIZE')&quot;) or die (mysql_error()); ;

                         ShowCart();
}//end function

function RemoveItem($cartId)

                          {
                          // Uses an SQL delete statement to remove an item from
                          // the users cart

                          global $dbServer, $dbUser, $dbPass, $dbName;


                          // Get a connection to the database

                          $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());
                           
                           [COLOR=red]
                           //create the sql statement
                           $sql=&quot;delete from cart where cookieId = '&quot; . GetCartId() . &quot;' and cartId = $cartId&quot;;

                           //echo it out to test the statement for wellformedness
                           echo $sql; [/color]

                          //run the query
                          mysql_query($sql) or die(mysql_error());

                         }//end function
?>

hth



Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Thanks very much, Bastien.

Managed to solve it using what you suggested above as the basis for:

Code:
<?php

include(&quot;db.php&quot;);

if (!empty($_REQUEST['removeitem'])) { 
RemoveItem($_REQUEST['removeitem']); 
die();
 }

global $dbServer, $dbUser, $dbPass, $dbName;

// Get a connection to the database
$cxn = mysql_connect($dbServer, $dbUser, $dbPass, $dbName) or die              (mysql_error()); 

$db = mysql_select_db(&quot;kraffs5_actuacoscart&quot;, $cxn)  or die(mysql_error()); 
$sql = (&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values('&quot; . GetCartId() . &quot;', $ITEMID, $QTY, '$COLOR' ,'$SIZE')&quot;) or die(mysql_error());

@mysql_query(&quot;insert into cart(cookieId, ITEMID, QTY,COLOR,SIZE) values('&quot; . GetCartId() . &quot;', $ITEMID, $QTY, '$COLOR' ,'$SIZE')&quot;) or die (mysql_error()); ;

ShowCart();


 function RemoveItem($cartId)

{
 global $dbServer, $dbUser, $dbPass, $dbName;

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName) or die(mysql_error());
                     
 //create the sql statement
$sql=&quot;delete from cart where cookieId = '&quot; . GetCartId() . &quot;' and cartId = $cartId&quot;;

//echo it out to test the statement for wellformedness
// echo $sql; 

//run the query
mysql_query($sql) or die(mysql_error());

ShowCart();


}//end function


	function ShowCart()
	{
BLAH etcetra>>>>>>>>>>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top