Jump to content

Else parse Error :'( Please Help


simpso

Recommended Posts

HI everyone

I am currently attempting to input the below code which will work when a user clicks on a link, however i am getting a parse else error when i insert the Insert into line.

Anyone got any ideas, im sure is something simple but im new to this.

 

Thanks

 

<?php do { ?>

<a href="AddToCartTest.php?id=<?php echo $row_ItemsList['ID']; ?>"><?php echo $row_ItemsList['ID']; ?>

<?php if(isset($_COOKIE['CartID']))

$CartID= $_COOKIE['CartID'];

mysql_query("INSERT INTO 'cart' ('User_ID') VALUES ('$CartID')");

else

setcookie('CartID',RAND());

mysql_query("INSERT INTO 'cart' ('User_ID') VALUES ('$CartID')"); ?> </a>

<p>

Link to comment
Share on other sites

You only posted 9 lines of code. So, either there is more code - in which case you should have pointed out which one is line 69 - or the error is in a different file.

 

But, as Jessica pointed out you don't have containing curly braces around the code for your if/else conditions.

 

For an IF statement, it is valid to have the condition followed by a SINGLE statement without any grouping. But, if you have multiple statements to go with a condition you must create a statement group. Also, it is not valid (pretty sure) to have an ELSE statement without some type of structure to separate the code appropriately.

 

I suggest you take a look at the following three pages in the manual (they are not long):

http://php.net/manua...ructures.if.php

http://php.net/manua...ctures.else.php

http://php.net/manua...ures.elseif.php

 

Also, you have an error in your query - table/field names should be enclosed in BACK quotes, not strait quotes.Values/strings should be enclosed in strait quotes. But, you don't need to enclose table/field names in quotes unless they are reserved words or contain characters such as a space - but you shoudl avoid those anyways.

 mysql_query("INSERT INTO `cart` (`User_ID`) VALUES ('$CartID')");

Edited by Psycho
Link to comment
Share on other sites

Ok i have placed the curly brackets inand attempted the backwards quotes but still getting an error

It jumps between unexpected ; on line 69 and when i take the ; out parse t string error.

 

Code:

 

<div id = "Test"><?php do { ?>

<a href="AddToCartTest.php?id=<?php echo $row_ItemsList['ID']; ?>"><?php echo $row_ItemsList['ID']; ?>

<?php if(isset($_COOKIE['CartID']))

{$CartIDs= $_COOKIE['CartID'];

mysql_query("INSERT INTO 'cart' ('User_ID') VALUES ('$CartID')");}

else

LINE69 {(setcookie('CartID', rand())

mysql_query("INSERT INTO cart (User_ID) VALUES ('$CartID')"); ?> </a>

<p>

<?php } while ($row_ItemsList = mysql_fetch_assoc($ItemsList));

 

echo $CartIDs;

?>

Link to comment
Share on other sites

He has an if, it's just hard to read the code. He's missing the closing bracket on the else, and a ; at the end of a line. And a bunch of other syntax errors I'm sure.

 

 

Properly formatted it would look more like:

<div id = "Test">
<?php 
do { 
echo '<a href="AddToCartTest.php?id='.$row_ItemsList['ID'].'">'.$row_ItemsList['ID'];

if(isset($_COOKIE['CartID'])){
	$CartIDs = $_COOKIE['CartID'];
	mysql_query("INSERT INTO 'cart' ('User_ID') VALUES ('$CartID')");
}else{
	setcookie('CartID', rand()); //missing ; and extra ( ?? wtf.
   	mysql_query("INSERT INTO cart (User_ID) VALUES ('$CartID')"); 
} // missing }

echo '</a>';

} while ($row_ItemsList = mysql_fetch_assoc($ItemsList));
?>

Edited by Jessica
Link to comment
Share on other sites

@simpso:

 

I would strongly advise you to NOT have your scripts jump in and out of PHP tags like you had and instead do something closer to what Jessica provided. Using the type of format you were using makes it very difficult to spot these types of problems. Also, in the future, please put [ code ] [ /code ] tags (without the spaces) around your code in the posts so it will be properly formatted (as you can see in Jessica's post above).

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.