simpso Posted January 2, 2013 Share Posted January 2, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/ Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 Including the error you get might help. However you're missing the { } around your if else statements. if($s){ //code //code }else{ //code //code } Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1402859 Share on other sites More sharing options...
simpso Posted January 2, 2013 Author Share Posted January 2, 2013 The error i am getting is : Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\LaughingSeagullV2\AddToCartTest.php on line 68 Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1402864 Share on other sites More sharing options...
Psycho Posted January 3, 2013 Share Posted January 3, 2013 (edited) 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 January 3, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1402876 Share on other sites More sharing options...
simpso Posted January 3, 2013 Author Share Posted January 3, 2013 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1403027 Share on other sites More sharing options...
Barand Posted January 3, 2013 Share Posted January 3, 2013 Read those links given to you earlier. An else must be preceded by an if Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1403029 Share on other sites More sharing options...
simpso Posted January 3, 2013 Author Share Posted January 3, 2013 Will do but i have an if up on line 65 <?php if(isset($_COOKIE['CartID'])) Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1403030 Share on other sites More sharing options...
Jessica Posted January 3, 2013 Share Posted January 3, 2013 (edited) 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 January 3, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1403031 Share on other sites More sharing options...
Psycho Posted January 3, 2013 Share Posted January 3, 2013 (edited) @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 January 3, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/272624-else-parse-error-please-help/#findComment-1403033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.