cty Posted February 4, 2007 Share Posted February 4, 2007 my Question: I am using MySQL+PHP5+IIS (*NOTE:i just let the user to buy each book for ONE unit only.) step 1(correct): when i click "Add to cart"(index.php) for the book "ABC" , a message was shown in (cart.php):"You have 1 item in your shopping basket" when i click the "Add to cart"(index.php) for the book "ABC" AGAIN, a message shown including:"You have 1 item in your shopping basket" and "you already order the book!" step 2(have bugs/error occur): when i click "when i click "Add to cart"(index.php) for the book "XYZ" , a message was shown in (cart.php):"You have 2 item in your shopping basket" when i click the "Add to cart"(index.php) for the book "ABC" AGAIN, a message shown including:"You have 3 item in your shopping basket" (*the item number will continue increase) what i want is:i hope i can get the message "You have 2 item in your shopping basket" Also,will shown "you already order the book!" when i try to add the same item again! The coding part that i suspect make the error occur as below: case 'add': if ($cart){ if($cart!=$_GET['id']){ $cart .= ','.$_GET['id']; } else{ echo "you already order the book!"; } } else{ $cart = $_GET['id']; } can anyone help me to edit it? Table Scheme: CREATE TABLE books( id int auto_increament, title varchar author varchar price decimal PRIMARY KEY(id) ); USE books; INSERT INTO books VALUES(1,'ABC','kelly','25.00'); INSERT INTO books VALUES(2,'XYZ','john','80.00'); EDITED BY WILDTEEN88: Please note your post has been moderated. All code snippets have been attached as files. Please use the code tags when posting code snippets. When posting code for whole files attach the files to your post instead. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/ Share on other sites More sharing options...
Destruction Posted February 4, 2007 Share Posted February 4, 2007 A little theory first. Say you have ABC and XYZ as you've used above, and you add it to the string each time you have... First order: $cart = 'ABC' Second order: $cart = 'ABC, XYZ' As you can see above, if you compare XYZ with $cart on your third order it will not be the same. You would be best using an array, something along the lines of this, before you start using $cart: <?php $cart = array(); ?> Then your code would be something like: <?php if(count($cart) > 0) { if(!in_array($_GET['id'], $cart)) { $cart[] = $_GET['id']; } else { echo "you already order the book!"; } } ?> This simply checks if there are any items in the array and if so, if any match $_GET['id']. This is a simple example that you can tailor to suit your needs but I would suggest using arrays in this instance. Dest Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/#findComment-176933 Share on other sites More sharing options...
cty Posted February 5, 2007 Author Share Posted February 5, 2007 hi,i try my code like this,but have warning! Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\testshop2\cart.php on line 17 Fatal error: [] operator not supported for strings in C:\testshop2\cart.php on line 19 PHP Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\testshop2\cart.php on line 17 PHP Fatal error: [] operator not supported for strings in C:\testshop2\cart.php on line 19 -------------------------------------------------------------------- switch ($action) { case 'add': if(count($cart) > 0) { if(!in_array($_GET['id'], $cart)) { $cart[] = $_GET['id']; } else { echo "you already order the book!"; } } else{ $cart = $_GET['id']; } break; Please try your best to help me.Thank you Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/#findComment-177219 Share on other sites More sharing options...
Destruction Posted February 5, 2007 Share Posted February 5, 2007 This would likely happen if $cart is not set as an array using $cart = array(). If you put something into it further up ie: from a session, make sure what you're putting into $cart is an array also. Dest Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/#findComment-177470 Share on other sites More sharing options...
cty Posted February 10, 2007 Author Share Posted February 10, 2007 can show me how to edit the code? Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/#findComment-181375 Share on other sites More sharing options...
cty Posted February 11, 2007 Author Share Posted February 11, 2007 can anyone help me to edit my coding? Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/#findComment-181808 Share on other sites More sharing options...
sasa Posted February 11, 2007 Share Posted February 11, 2007 try <?php session_start(); ?> <form method="POST"> <input type="text" name="id"> <input type="submit" value="add" name="action"> <input type="submit" value="clear cart" name="action"> </form> <?php if ($_POST['action'] == 'clear cart') unset($_SESSION['cart']); if (isset($_POST['action']) and $_POST['action'] == 'add') { $id = $_POST['id']; if ($_SESSION['cart'] and $id) { if (!in_array($id, $_SESSION['cart'])) $_SESSION['cart'][]=$id; else echo 'you already order the book!<br />'; } elseif ($id) $_SESSION['cart']= array($id); else echo 'no book <br />'; } echo 'In your card is: <br />'; if ($_SESSION['cart']) foreach ($_SESSION['cart'] as $a) echo $a,'<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/37050-coding-problemplease-help/#findComment-181883 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.