Jump to content

Coding problem,please help


cty

Recommended Posts

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />';
?>

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.